Home / Function/ test_create_summary_uses_get_buffer_string_format() — langchain Function Reference

test_create_summary_uses_get_buffer_string_format() — langchain Function Reference

Architecture documentation for the test_create_summary_uses_get_buffer_string_format() function in test_summarization.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a3b5be67_bf35_7571_1606_39c50deda23f["test_create_summary_uses_get_buffer_string_format()"]
  1911a463_b67d_0301_5ef1_5c535dafc14a["test_summarization.py"]
  a3b5be67_bf35_7571_1606_39c50deda23f -->|defined in| 1911a463_b67d_0301_5ef1_5c535dafc14a
  style a3b5be67_bf35_7571_1606_39c50deda23f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_summarization.py lines 1113–1165

def test_create_summary_uses_get_buffer_string_format() -> None:
    """Test that `_create_summary` formats messages using `get_buffer_string`.

    Ensures that messages are formatted efficiently for the summary prompt, avoiding
    token inflation from metadata when `str()` is called on message objects.

    This ensures the token count of the formatted prompt stays below what
    `count_tokens_approximately` estimates for the raw messages.
    """
    # Create messages with metadata that would inflate str() representation
    messages: list[AnyMessage] = [
        HumanMessage(content="What is the weather in NYC?"),
        AIMessage(
            content="Let me check the weather for you.",
            tool_calls=[{"name": "get_weather", "args": {"city": "NYC"}, "id": "call_123"}],
            usage_metadata={"input_tokens": 50, "output_tokens": 30, "total_tokens": 80},
            response_metadata={"model": "gpt-4", "finish_reason": "tool_calls"},
        ),
        ToolMessage(
            content="72F and sunny",
            tool_call_id="call_123",
            name="get_weather",
        ),
        AIMessage(
            content="It is 72F and sunny in NYC!",
            usage_metadata={
                "input_tokens": 100,
                "output_tokens": 25,
                "total_tokens": 125,
            },
            response_metadata={"model": "gpt-4", "finish_reason": "stop"},
        ),
    ]

    # Verify the token ratio is favorable (get_buffer_string < str)
    approx_tokens = count_tokens_approximately(messages)
    buffer_string = get_buffer_string(messages)
    buffer_tokens_estimate = len(buffer_string) / 4  # ~4 chars per token

    # The ratio should be less than 1.0 (buffer_string uses fewer tokens than counted)
    ratio = buffer_tokens_estimate / approx_tokens
    assert ratio < 1.0, (
        f"get_buffer_string should produce fewer tokens than count_tokens_approximately. "
        f"Got ratio {ratio:.2f}x (expected < 1.0)"
    )

    # Verify str() would have been worse
    str_tokens_estimate = len(str(messages)) / 4
    str_ratio = str_tokens_estimate / approx_tokens
    assert str_ratio > 1.5, (
        f"str(messages) should produce significantly more tokens. "
        f"Got ratio {str_ratio:.2f}x (expected > 1.5)"
    )

Domain

Subdomains

Frequently Asked Questions

What does test_create_summary_uses_get_buffer_string_format() do?
test_create_summary_uses_get_buffer_string_format() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_summarization.py.
Where is test_create_summary_uses_get_buffer_string_format() defined?
test_create_summary_uses_get_buffer_string_format() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_summarization.py at line 1113.

Analyze Your Own Codebase

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

Try Supermodel Free