Skip to content

fix(models): Correctly serialize nested schema types in LiteLlm adapter #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

PaveLuchkov
Copy link

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 the LiteLlm adapter, the schema generation produced invalid type representations like <Type.OBJECT: 'OBJECT'> instead of standard JSON schema types like "object". This resulted in litellm.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 within google.adk.models.lite_llm.py. It now uses a TYPE_LABELS dictionary lookup based on the types.Type enum value to ensure correct JSON schema type strings ('string', 'object', 'array', etc.) are generated for all nesting levels.

Testing:

  1. Manual Verification: Successfully reproduced the original BadRequestError using calendar_events_update and get_weather with google/gemini-2.0-flash-001 and openai/gpt-4o-mini via LiteLLM/OpenRouter. After applying the fix, ran the same code successfully. The error is resolved.
  2. Log Verification: Verified in LiteLLM debug logs that the tools payload sent to the LLM now contains the correct JSON schema types.
    * Before: ... "type": <Type.OBJECT: 'OBJECT'> ...
    * After: ... "type": "object" ...
  3. Automated Tests: Ran pytest locally (using python -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

image

PaveLuchkov and others added 2 commits May 1, 2025 12:09
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.

Fixes google#171
@hangfei hangfei requested a review from selcukgun May 3, 2025 00:12
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.

openrouter throws BadRequestError
1 participant