Home / Function/ test_state_schema_with_private_state_field() — langchain Function Reference

test_state_schema_with_private_state_field() — langchain Function Reference

Architecture documentation for the test_state_schema_with_private_state_field() function in test_state_schema.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5424f333_5da2_9eb4_898e_f9c6354a7e67["test_state_schema_with_private_state_field()"]
  3d4a3f09_0839_f785_dcfd_f1e7b13e3204["test_state_schema.py"]
  5424f333_5da2_9eb4_898e_f9c6354a7e67 -->|defined in| 3d4a3f09_0839_f785_dcfd_f1e7b13e3204
  style 5424f333_5da2_9eb4_898e_f9c6354a7e67 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/test_state_schema.py lines 201–255

def test_state_schema_with_private_state_field() -> None:
    """Test that private state fields (PrivateStateAttr) are filtered from input and output.

    Private state fields are marked with PrivateStateAttr annotation, which means:
    - They are omitted from the input schema (filtered out when invoking)
    - They are omitted from the output schema (filtered out from results)
    - Even if provided during invoke, they won't appear in state or results
    """

    class StateWithPrivateField(AgentState[Any]):
        public_field: str
        private_field: Annotated[str, PrivateStateAttr]

    captured_state = {}

    @tool
    def capture_state_tool(x: int, runtime: ToolRuntime) -> str:
        """Tool that captures the current state for inspection."""
        captured_state["state"] = dict(runtime.state)
        return f"Captured state with x={x}"

    agent = create_agent(
        model=FakeToolCallingModel(
            tool_calls=[
                [{"args": {"x": 42}, "id": "call_1", "name": "capture_state_tool"}],
                [],
            ]
        ),
        tools=[capture_state_tool],
        state_schema=StateWithPrivateField,
    )

    # Invoke the agent with BOTH public and private fields
    result = agent.invoke(
        {
            "messages": [HumanMessage("Test private state")],
            "public_field": "public_value",
            "private_field": "private_value",  # This should be filtered out
        }
    )

    # Assert that public_field is preserved in the result
    assert result["public_field"] == "public_value"

    # Assert that private_field is NOT in the result (filtered out from output)
    assert "private_field" not in result

    # Assert that private_field was NOT in the state during tool execution
    assert "private_field" not in captured_state["state"]

    # Assert that public_field WAS in the state during tool execution
    assert captured_state["state"]["public_field"] == "public_value"

    # Verify the agent executed normally
    assert len(result["messages"]) == 4  # Human, AI (tool call), Tool result, AI (final)

Domain

Subdomains

Frequently Asked Questions

What does test_state_schema_with_private_state_field() do?
test_state_schema_with_private_state_field() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/test_state_schema.py.
Where is test_state_schema_with_private_state_field() defined?
test_state_schema_with_private_state_field() is defined in libs/langchain_v1/tests/unit_tests/agents/test_state_schema.py at line 201.

Analyze Your Own Codebase

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

Try Supermodel Free