-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Type of issue
issue / bug
Language
Python
Description
All the examples for Response Format for the context engineering page are technically not correct (don't know if intended it to be wrong as it is sudo code anyways, but that's another question in itself).
https://docs.langchain.com/oss/python/langchain/context-engineering#response-format
@wrap_model_call
def state_based_output(
request: ModelRequest,
handler: Callable[[ModelRequest], ModelResponse]
) -> ModelResponse:
"""Select output format based on State."""
# request.messages is a shortcut for request.state["messages"]
message_count = len(request.messages)
if message_count < 3:
# Early conversation - use simple format
request = request.override(response_format=SimpleResponse)
else:
# Established conversation - use detailed format
request = request.override(response_format=DetailedResponse)
return handler(request)effectively does nothing to response_format downstream as no structured_response propagates to the ModelResponse. In these example, the response_format is the bare schema (the Meta BaseModel subclass) itself, but the response_format is expected to be one of the Strategy dataclasses and this schema is an attribute of this Strategy.
it's clear that the the logic surrounding the effective_response_format for the bounded model of the base handler _execute_model_(a)sync and how the output is handled/parsed - that the effective_response_format in factory.py is a ProviderStrategy or ToolStrategy object that depends on being one of these types when overriding the request's response_format.
so
from langchain.agents.structured_output import ProviderStrategy
@wrap_model_call
def state_based_output(
request: ModelRequest,
handler: Callable[[ModelRequest], ModelResponse]
) -> ModelResponse:
"""Select output format based on State."""
# request.messages is a shortcut for request.state["messages"]
message_count = len(request.messages)
if message_count < 3:
# Early conversation - use simple format
request = request.override(response_format=ProviderStrategy(schema=SimpleResponse)
else:
# Established conversation - use detailed format
request = request.override(response_format=ProviderStrategy(schema=DetailedResponse)
return handler(request)does work and structured_response (e.g. the filled SimpleReponse) in the ModelResponse does propagate. The override with ProviderStrategy works for any model and (even models with no native structured_output capabilities) and ToolStrategy is correctly rejected based on how the internal structured_output_tools are added, but that's just an aside and another aspect to note but not directly related to this docs inquiry.
thank you and I appreciate all the work you guys do.