Home / Class/ TestCustomStateField Class — langchain Architecture

TestCustomStateField Class — langchain Architecture

Architecture documentation for the TestCustomStateField class in test_wrap_model_call_state_update.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  39f408b3_5c60_c805_79c1_928b75d54186["TestCustomStateField"]
  e4070722_d3c6_f73d_d727_5779ab576a2a["AgentState"]
  39f408b3_5c60_c805_79c1_928b75d54186 -->|extends| e4070722_d3c6_f73d_d727_5779ab576a2a
  949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e["AgentMiddleware"]
  39f408b3_5c60_c805_79c1_928b75d54186 -->|extends| 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e
  08361e76_a995_1e57_f200_e292474e2f00["test_wrap_model_call_state_update.py"]
  39f408b3_5c60_c805_79c1_928b75d54186 -->|defined in| 08361e76_a995_1e57_f200_e292474e2f00
  c63f428e_384f_b63a_d89a_009ef771bae3["test_custom_field_via_state_schema()"]
  39f408b3_5c60_c805_79c1_928b75d54186 -->|method| c63f428e_384f_b63a_d89a_009ef771bae3
  2601e4e9_ef96_4d5d_8aa9_c1174c00f9f7["test_no_command()"]
  39f408b3_5c60_c805_79c1_928b75d54186 -->|method| 2601e4e9_ef96_4d5d_8aa9_c1174c00f9f7

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call_state_update.py lines 145–195

class TestCustomStateField:
    """Test ExtendedModelResponse with custom state fields defined via state_schema."""

    def test_custom_field_via_state_schema(self) -> None:
        """Middleware updates a custom state field via ExtendedModelResponse."""

        class MyState(AgentState):
            summary: str

        class SummaryMiddleware(AgentMiddleware):
            state_schema = MyState  # type: ignore[assignment]

            def wrap_model_call(
                self,
                request: ModelRequest,
                handler: Callable[[ModelRequest], ModelResponse],
            ) -> ExtendedModelResponse:
                response = handler(request)
                return ExtendedModelResponse(
                    model_response=response,
                    command=Command(update={"summary": "conversation summarized"}),
                )

        model = GenericFakeChatModel(messages=iter([AIMessage(content="Hello")]))
        agent = create_agent(model=model, middleware=[SummaryMiddleware()])

        result = agent.invoke({"messages": [HumanMessage("Hi")]})

        assert result["messages"][-1].content == "Hello"

    def test_no_command(self) -> None:
        """ExtendedModelResponse with no command works like ModelResponse."""

        class NoCommandMiddleware(AgentMiddleware):
            def wrap_model_call(
                self,
                request: ModelRequest,
                handler: Callable[[ModelRequest], ModelResponse],
            ) -> ExtendedModelResponse:
                response = handler(request)
                return ExtendedModelResponse(
                    model_response=response,
                )

        model = GenericFakeChatModel(messages=iter([AIMessage(content="Hello")]))
        agent = create_agent(model=model, middleware=[NoCommandMiddleware()])

        result = agent.invoke({"messages": [HumanMessage("Hi")]})

        assert len(result["messages"]) == 2
        assert result["messages"][1].content == "Hello"

Frequently Asked Questions

What is the TestCustomStateField class?
TestCustomStateField is a class in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call_state_update.py.
Where is TestCustomStateField defined?
TestCustomStateField is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call_state_update.py at line 145.
What does TestCustomStateField extend?
TestCustomStateField extends AgentState, AgentMiddleware.

Analyze Your Own Codebase

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

Try Supermodel Free