Home / Class/ TestDynamicModelWithResponseFormat Class — langchain Architecture

TestDynamicModelWithResponseFormat Class — langchain Architecture

Architecture documentation for the TestDynamicModelWithResponseFormat class in test_response_format.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  873688ef_76d2_d38f_3fb5_8b8c01c2a0c4["TestDynamicModelWithResponseFormat"]
  0f829642_e361_67a1_cbca_f82839bbbd31["GenericFakeChatModel"]
  873688ef_76d2_d38f_3fb5_8b8c01c2a0c4 -->|extends| 0f829642_e361_67a1_cbca_f82839bbbd31
  949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e["AgentMiddleware"]
  873688ef_76d2_d38f_3fb5_8b8c01c2a0c4 -->|extends| 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e
  de5a7878_b3fe_95d7_2575_7f534546dc1e["AIMessage"]
  873688ef_76d2_d38f_3fb5_8b8c01c2a0c4 -->|extends| de5a7878_b3fe_95d7_2575_7f534546dc1e
  2970c11e_b43a_df0c_1a52_30da10d3689c["test_response_format.py"]
  873688ef_76d2_d38f_3fb5_8b8c01c2a0c4 -->|defined in| 2970c11e_b43a_df0c_1a52_30da10d3689c
  3a8452d0_bc0b_b762_5ef2_4ace62e82c54["test_middleware_model_swap_provider_to_tool_strategy()"]
  873688ef_76d2_d38f_3fb5_8b8c01c2a0c4 -->|method| 3a8452d0_bc0b_b762_5ef2_4ace62e82c54

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/test_response_format.py lines 789–871

class TestDynamicModelWithResponseFormat:
    """Test response_format with middleware that modifies the model."""

    def test_middleware_model_swap_provider_to_tool_strategy(self) -> None:
        """Test that strategy resolution is deferred until after middleware modifies the model.

        Verifies that when a raw schema is provided, `_supports_provider_strategy` is called
        on the middleware-modified model (not the original), ensuring the correct strategy is
        selected based on the final model's capabilities.
        """

        # Custom model that we'll use to test whether the tool strategy is applied
        # correctly at runtime.
        class CustomModel(GenericFakeChatModel):
            tool_bindings: list[Any] = Field(default_factory=list)

            def bind_tools(
                self,
                tools: Sequence[dict[str, Any] | type[BaseModel] | Callable[..., Any] | BaseTool],
                **kwargs: Any,
            ) -> Runnable[LanguageModelInput, AIMessage]:
                # Record every tool binding event.
                self.tool_bindings.append(tools)
                return self

        model = CustomModel(
            messages=iter(
                [
                    # Simulate model returning structured output directly
                    # (this is what provider strategy would do)
                    json.dumps(WEATHER_DATA),
                ]
            )
        )

        # Create middleware that swaps the model in the request
        class ModelSwappingMiddleware(AgentMiddleware):
            def wrap_model_call(
                self,
                request: ModelRequest,
                handler: Callable[[ModelRequest], ModelResponse],
            ) -> ModelCallResult:
                # Replace the model with our custom test model
                return handler(request.override(model=model))

        # Track which model is checked for provider strategy support
        calls = []

        def mock_supports_provider_strategy(
            model: str | BaseChatModel, tools: list[Any] | None = None
        ) -> bool:
            """Track which model is checked and return True for ProviderStrategy."""
            calls.append(model)
            return True

        # Use raw Pydantic model (not wrapped in ToolStrategy or ProviderStrategy)
        # This should auto-detect strategy based on model capabilities
        agent = create_agent(
            model=model,
            tools=[],
            # Raw schema - should auto-detect strategy
            response_format=WeatherBaseModel,
            middleware=[ModelSwappingMiddleware()],
        )

        with patch(
            "langchain.agents.factory._supports_provider_strategy",
            side_effect=mock_supports_provider_strategy,
        ):
            response = agent.invoke({"messages": [HumanMessage("What's the weather?")]})

        # Verify strategy resolution was deferred: check was called once during _get_bound_model
        assert len(calls) == 1

        # Verify successful parsing of JSON as structured output via ProviderStrategy
        assert response["structured_response"] == EXPECTED_WEATHER_PYDANTIC
        # Two messages: Human input message and AI response with JSON content
        assert len(response["messages"]) == 2
        ai_message = response["messages"][1]
        assert isinstance(ai_message, AIMessage)
        # ProviderStrategy doesn't use tool calls - it parses content directly

Frequently Asked Questions

What is the TestDynamicModelWithResponseFormat class?
TestDynamicModelWithResponseFormat is a class in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/test_response_format.py.
Where is TestDynamicModelWithResponseFormat defined?
TestDynamicModelWithResponseFormat is defined in libs/langchain_v1/tests/unit_tests/agents/test_response_format.py at line 789.
What does TestDynamicModelWithResponseFormat extend?
TestDynamicModelWithResponseFormat extends GenericFakeChatModel, AgentMiddleware, AIMessage.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free