Fix: Gemini Adapter Stream Handling and :map Type Compliance for json_schema; Add JSON Mode Support for Gemini Adapter #105
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/2
function has been updated with a fallback clause for the:json_schema
mode.test :gemini json_schema gemini-2.5-flash-preview-04-17 stream partial array of objects
.Compliance with Gemini's
responseSchema
for Ecto:map
Types:responseSchema
(injson_schema
mode) requires that any nested object definition must include a non-emptyproperties
field. It does not appear to supportadditionalProperties
for defining generic maps with arbitrary keys in this mode. This makes Ecto's generic:map
and{:map, type}
types incompatible if directly translated.AllEctoTypes
schema withintest/instructor_test.exs
has been modified to use conditional compilation:mode: :json_schema
,AllEctoTypes
now defines itsnested_object
andnested_object_two
fields usingembeds_one
. These embedded schemas (InstructorTest.HelperSchemas.NestedObject
andInstructorTest.HelperSchemas.NestedObjectTwo
) have explicitly defined properties, thus satisfying Gemini's API requirements.AllEctoTypes
will continue to use the standardfield :nested_object, :map
andfield :nested_object_two, {:map, :string}
. This allows testing the handling of generic map types with providers that support them.expected
data 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". Thisraise
correctly 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.