adk-java icon indicating copy to clipboard operation
adk-java copied to clipboard

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

Open svetanis opened this issue 5 months ago • 1 comments

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):

  • Closes: #1265 (https://github.com/google/adk-java/issues/1265)
  • Related: #1265

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:

  • [x] I have added or updated unit tests for my change.
  • [x] 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

  • [x] I have read the CONTRIBUTING.md document.
  • [x] My pull request contains a single commit.
  • [x] I have performed a self-review of my own code.
  • [x] I have commented my code, particularly in hard-to-understand areas.
  • [x] I have added tests that prove my fix is effective or that my feature works.
  • [x] New and existing unit tests pass locally with my changes.
  • [x] I have manually tested my changes end-to-end.
  • [x] 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.

svetanis avatar Jun 13 '26 18:06 svetanis

Hi @svetanis, thank you for your contribution! We appreciate you taking the time to submit this pull request. I noticed that your changes are not covered by the current test cases and the test you included passes even without your changes. Could you please include the corresponding unit tests to verify your changes?

hemasekhar-p avatar Jun 15 '26 08:06 hemasekhar-p

Hello @hemasekhar-p ,

Thank you for the review and the feedback!

I've updated the PR and added three new dedicated unit tests in ChatCompletionsRequestTest that specifically target and verify the strict JSON Schema compliance fixes. The new tests ensure that:

  1. Missing function arguments are correctly serialized as empty objects ({}).
  2. Absent tool parameters are automatically populated with the required empty object schema.
  3. Gemini's uppercase schema types (like OBJECT and STRING) are safely normalized to lowercase to satisfy strict OpenAI API validation.

I have also successfully rebased the branch on top of the latest main to ensure there are no conflicts with the recent thoughtSignature updates. Everything is folded neatly into a single commit and all tests are passing locally.

Additionally, I ran comprehensive end-to-end testing on the rebased branch, and all of the demos (including a new one validating the thoughtSignature functionality) passed successfully.

Please let me know if there is anything else you need!

svetanis avatar Jun 15 '26 23:06 svetanis

@svetanis, thank you for addressing the comments. Currently this PR is under review by our team, we will keep you posted if any additional information is required. thank you.

hemasekhar-p avatar Jun 16 '26 09:06 hemasekhar-p

@anFatum, Could you please review this PR.

hemasekhar-p avatar Jun 16 '26 09:06 hemasekhar-p