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

vertexai: Allow json_mode in with_structured_output #533

Merged

Conversation

jzaldi
Copy link
Contributor

@jzaldi jzaldi commented Oct 4, 2024

PR Description

Adds a keyword parameter method='json_mode' to with_structured_output in ChatVertexAI to use controlled generation rather than function calling.

Relevant issues

Fixes #531, #503, #345, #144

Type

🆕 New Feature

Example

from pydantic import BaseModel
from langchain_google_vertexai import ChatVertexAI

class Response(BaseModel):
    response: str
    reason: str

model = (
    ChatVertexAI(model_name="gemini-1.5-pro-001")
    .with_structured_output(Response, method="json_mode")
)

model.invoke("What is the color of the sea?")

>> Response(response="The sea doesn't have a single color. It often appears blue due to the reflection of the sky, but it can also be green, turquoise, or even brown depending on depth, sediment, and the presence of microorganisms.", reason='The color of the sea is determined by the way it absorbs and reflects light, not an inherent pigment.')

@jzaldi
Copy link
Contributor Author

jzaldi commented Oct 4, 2024

We have an issue with nested pydantic models. The schema generated contains relative references and gemini api doesn't support that. I can implement a postprocessing function to get rid of refs in the meantime but is not ideal. @lkuligin wdyt?

@jzaldi
Copy link
Contributor Author

jzaldi commented Oct 4, 2024

Also I don't see any conflicts with what is being implemented in #469

@jzaldi jzaldi marked this pull request as ready for review October 4, 2024 21:13
@jzaldi
Copy link
Contributor Author

jzaldi commented Oct 4, 2024

Now we are only missing self referencing objects, something like this doesn't work and I don't think we can do anything about it if gemini's API doesn't support references:

class Status(BaseModel):
    substatus: "Status"

@lkuligin lkuligin merged commit c14d083 into langchain-ai:main Oct 5, 2024
14 of 15 checks passed
@alexminza
Copy link

This should also be added to ChatGoogleGenerativeAI

Reference: https://ai.google.dev/gemini-api/docs/structured-output

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.

[Feature Request] Use proper JSON Mode for Gemini Models instead of Tool calling
3 participants