| Name | Type | Description |
|---|---|---|
schema* | dict | type[BaseModel] | The output schema as a Pydantic |
method | Literal['function_calling', 'json_mode', 'json_schema'] | None | Default: 'json_schema'The method to use for structured output. Options:
|
include_raw | bool | Default: False |
Return a Runnable that constrains model output to a given schema.
Constrains the model to return output conforming to the provided schema.
Supports Pydantic models, TypedDict, and JSON schema dictionaries.
Example:
from pydantic import BaseModel
from langchain_google_genai import ChatGoogleGenerativeAI
class Person(BaseModel):
name: str
age: int
model = ChatGoogleGenerativeAI(model="gemini-3.1-pro-preview")
structured_model = model.with_structured_output(
Person,
method="json_schema",
)
result = structured_model.invoke(
"Tell me about a person named Alice, age 30"
)
print(result) # Person(name="Alice", age=30)
from pydantic import BaseModel
from langchain_google_genai import ChatGoogleGenerativeAI
class Recipe(BaseModel):
name: str
ingredients: list[str]
steps: list[str]
model = ChatGoogleGenerativeAI(model="gemini-3.1-pro-preview")
structured_model = model.with_structured_output(
Recipe, method="json_schema"
)
# Emits fully-parsed Recipe objects, not incremental JSON strings
for chunk in structured_model.stream(
"Give me a recipe for chocolate chip cookies"
):
print(chunk) # Recipe(name=..., ingredients=[...], steps=[...])
model = ChatGoogleGenerativeAI(model="gemini-3.1-pro-preview")
schema = {
"type": "object",
"properties": {
"title": {"type": "string"},
"priority": {"type": "integer"},
},
"required": ["title", "priority"],
}
structured_model = model.with_structured_output(
schema, method="json_schema"
)
result = structured_model.invoke("Create a task: finish report, priority 1")
print(result) # {"title": "finish report", "priority": 1}
structured_model = model.with_structured_output(
Person, method="json_schema", include_raw=True
)
result = structured_model.invoke("Tell me about Bob, age 25")
print(result["parsed"]) # Person(name="Bob", age=25)
print(result["raw"]) # AIMessage with full model responseIf True, returns a dict with both the raw model output
and the parsed structured output.