Skip to content
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

feat(gemini): support structured outputs #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mattmcdev
Copy link
Contributor

Created this PR before realising this one existed.

The main difference is this PR updates the structured output to match the schema format Google defines. It then passes this directly using response_schema rather than appending a message.

To me this seems the preferred way of doing it:

  • The schema matching the defined format is going to help the model know how to use your schema.
  • Passing the schema via response_schema means you get validation (and relevant errors) on your schema

@mattmcdev mattmcdev force-pushed the gemini-structured branch 3 times, most recently from 10ed5fb to fac51a4 Compare February 10, 2025 14:21
Copy link

kinsta bot commented Feb 12, 2025

Preview deployments for prism ⚡️

Status Branch preview Commit preview
✅ Ready Visit preview Visit preview

Commit: e1162ff023a34f722dd08f3730f464a5012946c4

Deployment ID: d871fe03-fdf3-4c20-8f31-f68cd128707a

Static site name: prism-97nz9

@ChrisB-TL
Copy link
Contributor

I'm not yet familiar with the Gemini API but if it accepts a proper schema that would seem the better route.

Do you know if there limitations at all, which would make the Anthropic adaption approach better?

We use the Anthropic adaption a lot, and whilst it works most of the time it isn't completely reliable. It goes off piste around 5% of the time.

@mattmcdev
Copy link
Contributor Author

I'm not yet familiar with the Gemini API but if it accepts a proper schema that would seem the better route.

Do you know if there limitations at all, which would make the Anthropic adaption approach better?

We use the Anthropic adaption a lot, and whilst it works most of the time it isn't completely reliable. It goes off piste around 5% of the time.

Google's docs seem to encourage using the schema via model configuration:

Important: This more formal method for declaring the JSON schema gives you more precise control than relying just on text in the prompt.

The only potential (sort of) downside is your call will fail if the schema doesn't pass validation which wouldn't happen with the Anthropic approach - personally I'd prefer to know that my schema was invalid and correct it.

@ChrisB-TL
Copy link
Contributor

Yea, I see that it would fail at the request level as an advantage - you don't get billed for the tokens for a broken request/response.

@mattmcdev
Copy link
Contributor Author

Yea, I see that it would fail at the request level as an advantage - you don't get billed for the tokens for a broken request/response.

Yep, and additionally I see the 'passing schemas as a message' type approach as a kind of hacky workaround for something that models seem to slowly be adopting more officially. So if there is an official implementation, probably better to work with it.

@pushpak1300
Copy link
Contributor

I think we should support both the implementation as both are supported by gemini at the moment. #173 similar like openai structred support.

src/Providers/OpenAI/Handlers/Structured.php

@benbjurstrom
Copy link

Seems like response_schema is the way to go here. What's the use case for supporting both implementations?

@mattmcdonald-uk appreciate the pull request. Just pulled it into a project and it's working great with gemini-2.0-flash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants