Support GEMINI_JSON and GEMINI_TOOLS modes with LiteLLM Router #2079
+330
−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 #1489
What changed
handle_gemini_jsonandhandle_gemini_toolsassumed the model is always bound at client creation time (the native Gemini SDK pattern viafrom_gemini()). When usingfrom_litellm()with a LiteLLM Router, the model is passed at request time in kwargs, which immediately triggered aConfigurationError.The fix detects the LiteLLM path by checking if
modelis present in kwargs:GEMINI_JSON (LiteLLM path):
response_formatwithtype: json_object(OpenAI style, which LiteLLM converts)update_gemini_kwargs()since LiteLLM handles message format conversion internallyGEMINI_TOOLS (LiteLLM path):
tool_choice(instead of Gemini-specificgemini_schemaandtool_config)update_gemini_kwargs()for the same reasonThe native Gemini SDK path (no
modelin kwargs) is completely unchanged.Changes
instructor/providers/gemini/utils.py: Branchhandle_gemini_jsonandhandle_gemini_toolsbased on whether model is in kwargs. Extract shared schema injection into_inject_json_schema_messagehelper.tests/llm/test_gemini_litellm_compat.py: 13 unit tests covering both GEMINI_JSON and GEMINI_TOOLS through the LiteLLM path, including the exact reproduction from Gemini specific Modes do not work with LiteLLM #1489.Testing
All 45 tests pass (13 new + 32 existing bedrock):
With this fix, the code from #1489 works as expected: