Skip to content

Add JSON Mode Support for Gemini Adapter #106

Description

@nshkrdotcom

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions