Model wrapper that returns outputs formatted to match the given schema.
with_structured_output(
self,
schema: dict[str, Any] | type | None = None,
*,
method: Literal['function_calling', 'json_mode', 'json_schema'] = 'function_calling',
include_raw: bool = False,
strict: bool | None = None,
**kwargs: Any = {}
) -> Runnable[LanguageModelInput, dict[str, Any] | BaseModel]schema=Pydantic class, method='function_calling', include_raw=Truefrom langchain_ibm import ChatWatsonx
from pydantic import BaseModel
class AnswerWithJustification(BaseModel):
'''An answer to the user question along with justification for the answer.'''
answer: str
justification: str
model = ChatWatsonx(...)
structured_model = model.with_structured_output(
AnswerWithJustification, include_raw=True
)
structured_model.invoke(
"What weighs more a pound of bricks or a pound of feathers"
)
{
"raw": AIMessage(
content="",
additional_kwargs={
"tool_calls": [
{
"id": "call_Ao02pnFYXD6GN1yzc0uXPsvF",
"function": {
"arguments": '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}',
"name": "AnswerWithJustification",
},
"type": "function",
}
]
},
),
"parsed": AnswerWithJustification(
answer="They weigh the same.",
justification="Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.",
),
"parsing_error": None,
}schema=JSON schema, method='function_calling', include_raw=Falsefrom langchain_ibm import ChatWatsonx
from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool
class AnswerWithJustification(BaseModel):
'''An answer to the user question along with justification for the answer.'''
answer: str
justification: str
dict_schema = convert_to_openai_tool(AnswerWithJustification)
model = ChatWatsonx(...)
structured_model = model.with_structured_output(dict_schema)
structured_model.invoke(
"What weighs more a pound of bricks or a pound of feathers"
)
{
"answer": "They weigh the same",
"justification": "Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.",
}schema=Pydantic class, method='json_schema', include_raw=Truefrom langchain_ibm import ChatWatsonx
from pydantic import BaseModel
class AnswerWithJustification(BaseModel):
'''An answer to the user question along with justification for the answer.'''
answer: str
justification: str
model = ChatWatsonx(...)
structured_model = model.with_structured_output(
AnswerWithJustification, method="json_schema", include_raw=True
)
structured_model.invoke(
"What weighs more a pound of bricks or a pound of feathers"
)
{
"raw": AIMessage(
content="",
additional_kwargs={
"tool_calls": [
{
"id": "chatcmpl-tool-bfbd6f6dd33b438990c5ddf277485971",
"type": "function",
"function": {
"name": "AnswerWithJustification",
"arguments": '{"answer": "They weigh the same", "justification": "A pound is a unit of weight or mass, so both a pound of bricks and a pound of feathers weigh the same amount, one pound."}',
},
}
]
},
response_metadata={
"token_usage": {
"completion_tokens": 45,
"prompt_tokens": 275,
"total_tokens": 320,
},
"model_name": "meta-llama/llama-3-3-70b-instruct",
"system_fingerprint": "",
"finish_reason": "stop",
},
id="chatcmpl-461ca5bd-1982-412c-b886-017c483bf481---8c18b06eead65ae4691364798787bda7---71896588-efa5-439f-a25f-d1abfe289f5a",
tool_calls=[
{
"name": "AnswerWithJustification",
"args": {
"answer": "They weigh the same",
"justification": "A pound is a unit of weight or mass, so both a pound of bricks and a pound of feathers weigh the same amount, one pound.",
},
"id": "chatcmpl-tool-bfbd6f6dd33b438990c5ddf277485971",
"type": "tool_call",
}
],
usage_metadata={
"input_tokens": 275,
"output_tokens": 45,
"total_tokens": 320,
},
),
"parsed": AnswerWithJustification(
answer="They weigh the same",
justification="A pound is a unit of weight or mass, so both a pound of bricks and a pound of feathers weigh the same amount, one pound.",
),
"parsing_error": None,
}schema=function schema, method='json_schema', include_raw=Falsefrom langchain_ibm import ChatWatsonx
from pydantic import BaseModel
function__schema = {
"name": "AnswerWithJustification",
"description": "An answer to the user question along with justification for the answer.",
"parameters": {
"type": "object",
"properties": {
"answer": {"type": "string"},
"justification": {
"description": "A justification for the answer.",
"type": "string",
},
},
"required": ["answer"],
},
}
model = ChatWatsonx(...)
structured_model = model.with_structured_output(
function_schema, method="json_schema", include_raw=False
)
structured_model.invoke(
"What weighs more a pound of bricks or a pound of feathers"
)
{
"answer": "They weigh the same",
"justification": "Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.",
}schema=Pydantic class, method='json_mode', include_raw=Truefrom langchain_ibm import ChatWatsonx
from pydantic import BaseModel
class AnswerWithJustification(BaseModel):
answer: str
justification: str
model = ChatWatsonx(...)
structured_model = model.with_structured_output(
AnswerWithJustification, method="json_mode", include_raw=True
)
structured_model.invoke(
"Answer the following question. "
"Make sure to return a JSON blob with keys 'answer' and 'justification'.\n\n"
"What's heavier a pound of bricks or a pound of feathers?"
)
{
"raw": AIMessage(
content='{\n "answer": "They are both the same weight.",\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \n}'
),
"parsed": AnswerWithJustification(
answer="They are both the same weight.",
justification="Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.",
),
"parsing_error": None,
}schema=None, method='json_mode', include_raw=Truefrom langchain_ibm import ChatWatsonx
model = ChatWatsonx(...)
structured_model = model.with_structured_output(
method="json_mode", include_raw=True
)
structured_model.invoke(
"Answer the following question. "
"Make sure to return a JSON blob with keys 'answer' and 'justification'.\n\n"
"What's heavier a pound of bricks or a pound of feathers?"
)
{
"raw": AIMessage(
content='{\n "answer": "They are both the same weight.",\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \n}'
),
"parsed": {
"answer": "They are both the same weight.",
"justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.",
},
"parsing_error": None,
}| Name | Type | Description |
|---|---|---|
schema | dict[str, Any] | type | None | Default: NoneThe output schema. Can be passed in as:
If |
method | Literal['function_calling', 'json_mode', 'json_schema'] | Default: 'function_calling'The method for steering model generation, one of:
|
include_raw | bool | Default: FalseIf False then only the parsed structured output is returned. If
an error occurs during model output parsing it will be raised. If True
then both the raw model response (a BaseMessage) and the parsed model
response will be returned. If an error occurs during output parsing it
will be caught and returned as well. The final output is always a dict
with keys |
strict | bool | None | Default: None
|
kwargs | Any | Default: {}Additional keyword args |