Skip to content

Instantly share code, notes, and snippets.

@perryism
Created April 9, 2025 21:15
Show Gist options
  • Save perryism/ace246533ab439bb6ebc018a3a6d3345 to your computer and use it in GitHub Desktop.
Save perryism/ace246533ab439bb6ebc018a3a6d3345 to your computer and use it in GitHub Desktop.
Gemini with structured output using langchain
from langchain_google_genai import ChatGoogleGenerativeAI
import os
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash-001",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
)
from pydantic import BaseModel
class Sentence(BaseModel):
original: str
translated: str
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
structured_llm = llm.with_structured_output(Sentence)
ai_msg = structured_llm.invoke(messages)
ai_msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment