Fix: Gemini Adapter Stream Handling and :map Type Compliance for json_schema; Add JSON Mode Support for Gemini Adapter#105
Open
nshkrdotcom wants to merge 3 commits intothmsmlr:mainfrom
Open
Conversation
added 2 commits
May 13, 2025 13:35
- Adds fallback for non-data chunks in Gemini :json_schema streaming,
preventing FunctionClauseErrors.
- Updates AllEctoTypes test to use conditional schema definition:
- For Gemini :json_schema, uses embeds_one for nested objects
to comply with API's non-empty 'properties' requirement.
- For other adapters/modes, retains generic :map type for testing.
- Slightly rewords Gemini adapter's client-side schema validation error
for clarity regarding naked :map types.
This ensures Gemini :json_schema tests pass for streaming and complex
types, while maintaining appropriate testing for other providers.
Addresses thmsmlr#104.
- Added `:json` to `@supported_modes` in `lib/instructor/adapters/gemini.ex`. - Updated `chat_completion` to set `response_mime_type: "application/json"` and remove `response_format` for `:json` mode. - Implemented `parse_response_for_mode/2` and `parse_stream_chunk_for_mode/2` for `:json` mode to handle JSON decoding and streaming. - Included preparatory `:tools` mode handling in `chat_completion` (not fully implemented). - Updated tests in `test/instructor_test.exs` to cover `:json` mode for Gemini. Note: One streaming test (`streams arrays one at a time`) fails due to invalid JSON output; this will be addressed in a follow-up issue. Fixes thmsmlr#106
Author
Description:This PR implements Changes:
Testing:
Known Issues:
Related Issue:Closes #106 |
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.
This PR resolves two issues related to the Gemini adapter, specifically when operating in
mode: :json_schema:Robust Stream Chunk Parsing (Fixes
FunctionClauseError):Instructor.Adapters.Gemini.parse_stream_chunk_for_mode/2function has been updated with a fallback clause for the:json_schemamode.test :gemini json_schema gemini-2.5-flash-preview-04-17 stream partial array of objects.Compliance with Gemini's
responseSchemafor Ecto:mapTypes:responseSchema(injson_schemamode) requires that any nested object definition must include a non-emptypropertiesfield. It does not appear to supportadditionalPropertiesfor defining generic maps with arbitrary keys in this mode. This makes Ecto's generic:mapand{:map, type}types incompatible if directly translated.AllEctoTypesschema withintest/instructor_test.exshas been modified to use conditional compilation:mode: :json_schema,AllEctoTypesnow defines itsnested_objectandnested_object_twofields usingembeds_one. These embedded schemas (InstructorTest.HelperSchemas.NestedObjectandInstructorTest.HelperSchemas.NestedObjectTwo) have explicitly defined properties, thus satisfying Gemini's API requirements.AllEctoTypeswill continue to use the standardfield :nested_object, :mapandfield :nested_object_two, {:map, :string}. This allows testing the handling of generic map types with providers that support them.expecteddata and the user prompt within the "all ecto types" test are also dynamically adjusted to match the schema variant being used for that specific test run.Instructor.Adapters.Gemini.normalize_json_schema/1(which raises if a schema would result in an object with emptyproperties) has been slightly reworded for better clarity regarding the use of "naked :map type". Thisraisecorrectly prevents sending an invalid schema to Gemini.test :gemini json_schema gemini-2.5-flash-preview-04-17 all ecto types.This approach ensures that the Gemini adapter functions correctly for streaming and handles its specific schema requirements gracefully, while also allowing the test suite to accurately test different schema interpretations across various providers in the future.
Closes #104.