fix(models): Correctly serialize nested schema types in LiteLlm adapter #486
+43
−37
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes conversion of Gemini Type enums within the recursive _schema_to_dict function to ensure valid JSON Schema types ('string', 'object', etc.) are generated, resolving errors with complex tools like Google Calendar while using LiteLLM and Openrouter.
Fixes #171
Problem:
When using complex tools with nested schemas (e.g., Google Calendar
insert
/update
) via theLiteLlm
adapter, the schema generation produced invalid type representations like<Type.OBJECT: 'OBJECT'>
instead of standard JSON schema types like"object"
. This resulted inlitellm.BadRequestError: ... Provider returned error (INVALID_ARGUMENT)
from the backend (e.g., Gemini via OpenRouter) due to schema validation failure.Solution:
This PR corrects the recursive
_schema_to_dict
function withingoogle.adk.models.lite_llm.py
. It now uses aTYPE_LABELS
dictionary lookup based on thetypes.Type
enum value to ensure correct JSON schema type strings ('string'
,'object'
,'array'
, etc.) are generated for all nesting levels.Testing:
BadRequestError
usingcalendar_events_update
andget_weather
withgoogle/gemini-2.0-flash-001
andopenai/gpt-4o-mini
via LiteLLM/OpenRouter. After applying the fix, ran the same code successfully. The error is resolved.tools
payload sent to the LLM now contains the correct JSON schema types.* Before:
... "type": <Type.OBJECT: 'OBJECT'> ...
* After:
... "type": "object" ...
pytest
locally (usingpython -X utf8 -m tests/unittests/models/test_litellm.py
). Result:36 passed, 12 skipped, 22 warnings
, confirming no regressions in the tested modules.Evidence:
The attached image shows success on the original problem from #171