Skip to content

fix: support typed responseSchema in ChatCompletionsRequest - #1447

Open
hemasekhar-p wants to merge 1 commit into
google:mainfrom
hemasekhar-p:issue1444-fix
Open

fix: support typed responseSchema in ChatCompletionsRequest#1447
hemasekhar-p wants to merge 1 commit into
google:mainfrom
hemasekhar-p:issue1444-fix

Conversation

@hemasekhar-p

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

Link to an existing issue (if applicable):

Problem:
When setting an output schema via LlmRequest.Builder.outputSchema(Schema) or LlmAgent.outputSchema(Schema), the schema is stored in GenerateContentConfig.responseSchema with responseMimeType = "application/json". In ChatCompletionsRequest.handleConfigOptions(...), only config.responseJsonSchema() was checked. Because responseJsonSchema is empty when using typed schemas, ChatCompletionsRequest fell back to responseMimeType.equals("application/json") and downgraded the request's response_format to {"type": "json_object"}. As a result, the defined schema was ignored and dropped from the OpenAI-compatible /chat/completions request.

Solution:

  • Added a check in ChatCompletionsRequest.handleConfigOptions(...) for config.responseSchema().isPresent() when config.responseJsonSchema() is absent.
  • Serialized the typed Schema into an OpenAI-compatible ResponseFormatJsonSchema ({"type": "json_schema", "json_schema": {"name": "response_schema", "strict": true, "schema": ...}}) using objectMapper.convertValue(...).
  • Retained precedence for explicitly provided raw schemas (config.responseJsonSchema()).

Testing Plan

Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Manual End-to-End (E2E) Tests:

Built and verified a reproduction test harness that creates an LlmRequest with an outputSchema(Schema), converts it via ChatCompletionsRequest.fromLlmRequest(request, false), and serializes it with Jackson:
Before Fix: Serialized output emitted {"type": "json_object"} without the schema.
After Fix: Serialized output emits {"type": "json_schema", "json_schema": {...}} preserving all defined fields (rootCause, confidence) and strict: true.

Checklist

  • I have read the CONTRIBUTING.md document.
  • My pull request contains a single commit.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

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.

ChatCompletionsRequest ignores typed responseSchema and downgrades outputSchema to json_object

1 participant