Skip to content

fix: Enforce strict JSON Schema compliance to prevent OpenAI 400 Bad Request errors#1266

Open
svetanis wants to merge 1 commit into
google:mainfrom
svetanis:fix-openai-json-schema-compliance
Open

fix: Enforce strict JSON Schema compliance to prevent OpenAI 400 Bad Request errors#1266
svetanis wants to merge 1 commit into
google:mainfrom
svetanis:fix-openai-json-schema-compliance

Conversation

@svetanis

Copy link
Copy Markdown

Enforce strict JSON Schema compliance to prevent OpenAI 400 Bad Request errors

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

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:
When using strict OpenAI-compatible providers like Groq, empty function arguments or responses are serialized as null or omitted by ChatCompletionsRequest. Furthermore, zero-argument tools fail to declare a parameters schema object. This breaks strict JSON schema parsers, causing them to drop conversation history, which leads to 400 Bad Request exceptions due to models hallucinating raw XML <function> tags. JSON Schema enum types are also improperly serialized in uppercase.

Solution:

  • Added a custom schemaNormalizerModule to the ObjectMapper in ChatCompletionsRequest.java to force Type enums to serialize in lowercase (e.g., "string").
  • Added enforceJsonObject to ChatCompletionsCommon.java to guarantee that empty arguments and function response payloads fallback to "{}" instead of null or raw strings.
  • Updated ChatCompletionsRequest.java to explicitly inject a {"type":"object", "properties":{}} parameters schema for zero-argument functions instead of omitting it.

Testing Plan

Unit Tests:

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

Passed: mvn test
Added testFromLlmRequest_withEmptyFunctionArguments to ChatCompletionsRequestTest.java to explicitly test "{}" serialization for zero-argument tools. Updated testFromLlmRequest_withFunctionResponse expectations.

Manual End-to-End (E2E) Tests:

Wired the Google native chat completion client into the external model-prism project (PR #1199). Ran the integration through all the demo tests covering all main ADK features. The applications successfully execute multi-turn multi-tool conversations using strict JSON schema endpoints without throwing 400 Bad Request due to context loss.

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.

Additional context

This change unlocks robust support for the rapidly growing ecosystem of OpenAI-compatible endpoints that enforce strict JSON Schema validation.

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.

Tool calls fail with 400 Bad Request due to strict JSON Schema violations (OpenAI)

1 participant