- 
                Notifications
    You must be signed in to change notification settings 
- Fork 93
Implement native JSON mode with schema for Gemini #983
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -145,13 +145,17 @@ def setup_call( | |
| Part.model_validate(part) for part in message_parts | ||
| ] | ||
|  | ||
| if json_mode: | ||
| if json_mode or (response_model and getattr(response_model, "model_config", {}).get("strict", False)): | ||
| with _generate_content_config_context(call_kwargs) as config: | ||
| if not tools: | ||
| config.response_mime_type = "application/json" | ||
| messages[-1]["parts"].append( # pyright: ignore [reportTypedDictNotRequiredAccess, reportOptionalMemberAccess, reportArgumentType] | ||
| PartDict(text=_utils.json_mode_content(response_model)) | ||
| ) # pyright: ignore [reportTypedDictNotRequiredAccess, reportOptionalMemberAccess, reportArgumentType] | ||
| config.response_mime_type = "application/json" | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was previously inside of the  | ||
| if response_model: | ||
| config.response_schema = response_model | ||
| elif not tools: | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why move this inside of this check for  | ||
| messages[-1][ | ||
| "parts" | ||
| ].append( # pyright: ignore [reportTypedDictNotRequiredAccess, reportOptionalMemberAccess, reportArgumentType] | ||
| PartDict(text=_utils.json_mode_content(None)) | ||
| ) # pyright: ignore [reportTypedDictNotRequiredAccess, reportOptionalMemberAccess, reportArgumentType] | ||
| elif response_model: | ||
| assert tool_types, "At least one tool must be provided for extraction." | ||
| with _generate_content_config_context(call_kwargs) as config: | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the null check on
response_modelshould guarantee the presence ofmodel_config, so you can do something like this: