[GH-ISSUE #1905] [langchain]: Response Format in the Context engineering doc #244

Open
opened 2026-02-17 17:19:29 -05:00 by yindo · 0 comments
Owner

Originally created by @hankbesser on GitHub (Dec 15, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/1905

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.

Originally created by @hankbesser on GitHub (Dec 15, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/1905 ### 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 ```python @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 ```python 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.
yindo added the langchainexternal labels 2026-02-17 17:19:29 -05:00
yindo changed title from [langchain]: `Response Format` in the Context engineering doc to [GH-ISSUE #1905] [langchain]: `Response Format` in the Context engineering doc 2026-06-05 17:25:50 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#244