Feature Request: Add support for :json mode in the Gemini adapter to enable generating JSON responses without a strict schema.
Description:
The Gemini adapter currently supports :json_schema mode for structured JSON output with a defined schema. This feature enhancement adds support for :json mode, which allows the Gemini API to generate JSON responses by setting response_mime_type: "application/json" without a response_schema. This is useful for scenarios where flexible JSON output is needed without enforcing a specific structure.
Proposed Changes:
- Update
lib/instructor/adapters/gemini.ex to include :json in @supported_modes.
- Modify the
chat_completion function to handle :json mode by setting response_mime_type: "application/json" and removing the response_format field to avoid API errors.
- Add response parsing for
:json mode in parse_response_for_mode/2 to decode JSON text.
- Add streaming support for
:json mode in parse_stream_chunk_for_mode/2 to handle JSON chunks.
- Update tests in
test/instructor_test.exs to include :json mode for the Gemini adapter (model: gemini-2.5-flash-preview-04-17).
Notes:
- The pull request includes preparatory changes for
:tools mode (e.g., mode handling in chat_completion), but these are not fully implemented or tested yet and will be addressed in a future issue.
- The implementation addresses a previous issue where
response_format caused 400 errors by explicitly removing it for :json mode.
- One streaming test (streams arrays one at a time) is failing due to invalid JSON output from the Gemini model. This may be addressed in a follow-up issue to improve prompt instructions or chunk cleaning.
Acceptance Criteria:
- All tests for
:json mode (except the known streaming test) pass with mix test --only adapter:gemini test/instructor_test.exs.
- The adapter correctly generates and parses JSON responses in non-streaming scenarios.
- Streaming responses are partially supported, with the known issue documented for future resolution.
Related Issues:
None yet, but a follow-up issue will be created for the streaming test failure and :tools mode implementation.
Feature Request: Add support for
:jsonmode in the Gemini adapter to enable generating JSON responses without a strict schema.Description:
The Gemini adapter currently supports
:json_schemamode for structured JSON output with a defined schema. This feature enhancement adds support for:jsonmode, which allows the Gemini API to generate JSON responses by settingresponse_mime_type: "application/json"without aresponse_schema. This is useful for scenarios where flexible JSON output is needed without enforcing a specific structure.Proposed Changes:
lib/instructor/adapters/gemini.exto include:jsonin@supported_modes.chat_completionfunction to handle:jsonmode by settingresponse_mime_type: "application/json"and removing theresponse_formatfield to avoid API errors.:jsonmode inparse_response_for_mode/2to decode JSON text.:jsonmode inparse_stream_chunk_for_mode/2to handle JSON chunks.test/instructor_test.exsto include:jsonmode for the Gemini adapter (model:gemini-2.5-flash-preview-04-17).Notes:
:toolsmode (e.g., mode handling inchat_completion), but these are not fully implemented or tested yet and will be addressed in a future issue.response_formatcaused 400 errors by explicitly removing it for:jsonmode.Acceptance Criteria:
:jsonmode (except the known streaming test) pass withmix test --only adapter:gemini test/instructor_test.exs.Related Issues:
None yet, but a follow-up issue will be created for the streaming test failure and
:toolsmode implementation.