Skip to content

fix error response format to match OpenAI API standard#1259

Open
sufubao wants to merge 1 commit intomainfrom
openai_fix
Open

fix error response format to match OpenAI API standard#1259
sufubao wants to merge 1 commit intomainfrom
openai_fix

Conversation

@sufubao
Copy link
Copy Markdown
Collaborator

@sufubao sufubao commented Apr 7, 2026

Summary

  • Wrap error responses in the standard {"error": {"message", "type", "param", "code"}} envelope to align with vLLM and OpenAI SDK expectations
  • Updated both create_error_response functions in api_http.py and api_openai.py
  • Auto-maps HTTP status codes to error types: 4xx → BadRequestError, 404 → NotFoundError, 5xx → InternalServerError

Background

The OpenAI Python SDK and most compatible clients parse response["error"]["message"] to extract error details. LightLLM's previous flat {"message": "..."} format caused KeyError exceptions in client error-handling code.

Test plan

  • Verify error responses from /v1/chat/completions with invalid parameters return the nested {"error": {...}} format
  • Verify error responses from /v1/completions return the same format
  • Verify OpenAI Python SDK correctly parses error responses without KeyError
  • Verify non-OpenAI endpoints in api_http.py also return the correct format

Wrap error responses in {"error": {"message", "type", "param", "code"}}
envelope to align with vLLM and OpenAI SDK expectations.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the create_error_response function in both api_http.py and api_openai.py to return a structured error object containing a message, type, parameter, and status code. The feedback recommends converting the code field from an integer to a string to ensure compatibility with the OpenAI API specification. Additionally, it is suggested to centralize the duplicated error response logic into a shared utility module to reduce maintenance overhead and prevent logic divergence.

g_objs.metric_client.counter_inc("lightllm_request_failure")
return JSONResponse({"message": message}, status_code=status_code.value)
return JSONResponse(
{"error": {"message": message, "type": err_type, "param": param, "code": status_code.value}},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The OpenAI API specification typically expects the code field to be a string or null. Using an integer might cause issues with some client libraries that strictly validate types. Converting the status code to a string is a safer approach and matches the behavior of other OpenAI-compatible servers like vLLM.

Suggested change
{"error": {"message": message, "type": err_type, "param": param, "code": status_code.value}},
{"error": {"message": message, "type": err_type, "param": param, "code": str(status_code.value)}},

Comment on lines +61 to +63
def create_error_response(
status_code: HTTPStatus, message: str, err_type: str = None, param: str = None
) -> JSONResponse:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function is a duplicate of the one implemented in api_http.py. Duplicating logic across files increases maintenance overhead and the risk of divergence. It is recommended to centralize this utility in a shared module. If circular dependencies between api_http.py and api_openai.py are a concern, consider moving the shared logic to a common utility file (e.g., lightllm/utils/api_utils.py) that both can import.

g_objs.metric_client.counter_inc("lightllm_request_failure")
return JSONResponse({"message": message}, status_code=status_code.value)
return JSONResponse(
{"error": {"message": message, "type": err_type, "param": param, "code": status_code.value}},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The code field should be a string to align with the OpenAI API standard, ensuring consistency with the implementation in api_http.py.

Suggested change
{"error": {"message": message, "type": err_type, "param": param, "code": status_code.value}},
{"error": {"message": message, "type": err_type, "param": param, "code": str(status_code.value)}},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant