Support GEMINI_JSON and GEMINI_TOOLS modes with LiteLLM Router#2079
Open
debu-sinha wants to merge 1 commit into567-labs:mainfrom
Open
Support GEMINI_JSON and GEMINI_TOOLS modes with LiteLLM Router#2079debu-sinha wants to merge 1 commit into567-labs:mainfrom
debu-sinha wants to merge 1 commit into567-labs:mainfrom
Conversation
Fixes 567-labs#1489. When using from_litellm() with Mode.GEMINI_JSON or Mode.GEMINI_TOOLS, the handler immediately raised ConfigurationError because it required the model to be set at patch time (the native Gemini SDK pattern), not at request time (the LiteLLM pattern). The fix detects the LiteLLM path by checking if "model" is in kwargs. When it is: - GEMINI_JSON: injects JSON schema into system message and sets response_format (OpenAI style), skips Gemini message conversion - GEMINI_TOOLS: uses OpenAI-style tool definitions and tool_choice, skips Gemini message conversion The native Gemini SDK path (no model in kwargs) is completely unchanged. LiteLLM handles its own message format conversion internally, so we just need to set up the right parameters. Signed-off-by: debu-sinha <debusinha2009@gmail.com>
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: