Model wrapper that returns outputs formatted to match the given schema.
See the LangChain docs for more details and examples.
with_structured_output(
self,
schema: dict | type,
*,
include_raw: bool = False,
method: Literal['function_calling', 'json_schema'] = 'function_calling',
**kwargs: Any = {}
) -> Runnable[LanguageModelInput, dict | BaseModel]Example:
from langchain_anthropic import ChatAnthropic
from pydantic import BaseModel, Field
model = ChatAnthropic(model="claude-sonnet-4-5")
class Movie(BaseModel):
"""A movie with details."""
title: str = Field(..., description="The title of the movie")
year: int = Field(..., description="The year the movie was released")
director: str = Field(..., description="The director of the movie")
rating: float = Field(..., description="The movie's rating out of 10")
model_with_structure = model.with_structured_output(Movie, method="json_schema")
response = model_with_structure.invoke("Provide details about the movie Inception")
print(response)
# -> Movie(title="Inception", year=2010, director="Christopher Nolan", rating=8.8)| Name | Type | Description |
|---|---|---|
schema* | dict | type | The output schema. Can be passed in as:
If See |
include_raw | bool | Default: FalseIf If an error occurs during model output parsing it will be raised. If If an error occurs during output parsing it will be caught and returned as well. The final output is always a |
method | Literal['function_calling', 'json_schema'] | Default: 'function_calling'The structured output method to use. Options are:
|
kwargs | Any | Default: {}Additional keyword arguments are ignored. |