Skip to content

[router][grpc] Fix error message format in grpc chat handler#11307

Merged
slin1237 merged 3 commits intomainfrom
chang/error-msg
Oct 7, 2025
Merged

[router][grpc] Fix error message format in grpc chat handler#11307
slin1237 merged 3 commits intomainfrom
chang/error-msg

Conversation

@CatherineSue
Copy link
Collaborator

@CatherineSue CatherineSue commented Oct 7, 2025

Motivation

This PR makes sure the error response from grpc router chat completions endpoint matches the OpenAI standard.

Modifications

  • Added error helper functions to src/routers/grpc/utils.rs:
    • internal_error_static() - for internal errors with static messages
    • internal_error_message() - for internal errors with dynamic messages
    • bad_request_error() - for 400 bad request errors
    • service_unavailable_error() - for 503 service unavailable errors
  • Updated error responses in both routers (router.rs and pd_router.rs):
    • All error responses in route_chat_impl now use JSON format
    • All error responses in route_generate_impl now use JSON format
    • All error responses in streaming handlers now use JSON format
    • Updated get_grpc_client_from_worker in utils to use JSON format
  • All errors now follow this JSON structure:
{
 "error": {
   "message": "error message here",
   "type": "error_type",
   "code": http_status_code
 }
}

Accuracy Tests

Screenshot 2025-10-07 at 1 39 05 PM

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @CatherineSue, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request standardizes the error response format for the gRPC chat completions endpoint to align with the OpenAI specification. It achieves this by centralizing error handling logic into new utility functions that generate structured JSON error messages, and then refactoring the existing router implementations to use these new helpers. This change ensures consistent and predictable error reporting across the gRPC interface.

Highlights

  • Standardized Error Format: All gRPC chat completion error responses now conform to an OpenAI-like JSON structure, including "message", "type", and "code" fields.
  • New Error Utility Functions: Introduced internal_error_static, internal_error_message, bad_request_error, and service_unavailable_error in src/routers/grpc/utils.rs for consistent error generation.
  • Refactored Error Handling: Existing error handling logic in router.rs and pd_router.rs has been updated to utilize these new centralized utility functions, reducing code duplication and improving maintainability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 effectively standardizes the error responses for the gRPC router endpoints to match the OpenAI format. The introduction of centralized error helper functions in src/routers/grpc/utils.rs is a great improvement for maintainability and consistency. The changes have been applied thoroughly across both pd_router.rs and router.rs, including non-streaming and streaming handlers. My review includes one suggestion to further refactor the new helper functions to reduce code duplication.

Comment on lines 410 to 468
pub fn internal_error_static(msg: &'static str) -> Response {
error!("{}", msg);
(StatusCode::INTERNAL_SERVER_ERROR, msg).into_response()
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({
"error": {
"message": msg,
"type": "internal_error",
"code": 500
}
})),
)
.into_response()
}

pub fn internal_error_message(message: String) -> Response {
error!("{}", message);
(StatusCode::INTERNAL_SERVER_ERROR, message).into_response()
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({
"error": {
"message": message,
"type": "internal_error",
"code": 500
}
})),
)
.into_response()
}

pub fn bad_request_error(message: String) -> Response {
error!("{}", message);
(
StatusCode::BAD_REQUEST,
Json(json!({
"error": {
"message": message,
"type": "invalid_request_error",
"code": 400
}
})),
)
.into_response()
}

pub fn service_unavailable_error(message: String) -> Response {
warn!("{}", message);
(
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"error": {
"message": message,
"type": "service_unavailable",
"code": 503
}
})),
)
.into_response()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

These new error helper functions are a great addition for standardizing error responses. However, there's a noticeable amount of code duplication among them. All four functions follow the same pattern: log a message, then construct and return a JSON response with a status code.

To improve maintainability and reduce redundancy, you could introduce a private helper function that encapsulates this common logic. This would make it easier to modify the error structure or logging behavior in the future. See the suggestion below for a possible refactoring.

fn create_error_response(
    status: StatusCode,
    error_type: &'static str,
    code: u16,
    message: String,
) -> Response {
    (
        status,
        Json(json!({
            "error": {
                "message": message,
                "type": error_type,
                "code": code
            }
        })),
    )
        .into_response()
}

pub fn internal_error_static(msg: &'static str) -> Response {
    error!("{}", msg);
    create_error_response(
        StatusCode::INTERNAL_SERVER_ERROR,
        "internal_error",
        500,
        msg.to_string(),
    )
}

pub fn internal_error_message(message: String) -> Response {
    error!("{}", message);
    create_error_response(
        StatusCode::INTERNAL_SERVER_ERROR,
        "internal_error",
        500,
        message,
    )
}

pub fn bad_request_error(message: String) -> Response {
    error!("{}", message);
    create_error_response(
        StatusCode::BAD_REQUEST,
        "invalid_request_error",
        400,
        message,
    )
}

pub fn service_unavailable_error(message: String) -> Response {
    warn!("{}", message);
    create_error_response(
        StatusCode::SERVICE_UNAVAILABLE,
        "service_unavailable",
        503,
        message,
    )
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Meh, will refactor later. After all, your implementation doesn't give much fewer lines.

@slin1237 slin1237 merged commit 420c99a into main Oct 7, 2025
31 of 33 checks passed
@slin1237 slin1237 deleted the chang/error-msg branch October 7, 2025 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments