Home / Function/ test_on_llm_end_stores_usage_metadata_in_run_extra() — langchain Function Reference

test_on_llm_end_stores_usage_metadata_in_run_extra() — langchain Function Reference

Architecture documentation for the test_on_llm_end_stores_usage_metadata_in_run_extra() function in test_langchain.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  682e0bc5_7414_bff9_64dc_95d3d7d3d624["test_on_llm_end_stores_usage_metadata_in_run_extra()"]
  6ee6ed10_3ff3_3d50_08bc_311ea99250db["test_langchain.py"]
  682e0bc5_7414_bff9_64dc_95d3d7d3d624 -->|defined in| 6ee6ed10_3ff3_3d50_08bc_311ea99250db
  style 682e0bc5_7414_bff9_64dc_95d3d7d3d624 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/tracers/test_langchain.py lines 252–285

def test_on_llm_end_stores_usage_metadata_in_run_extra() -> None:
    """Test that `usage_metadata` is stored in `run.extra.metadata` on llm end."""
    client = unittest.mock.MagicMock(spec=Client)
    client.tracing_queue = None
    tracer = LangChainTracer(client=client)

    run_id = UUID("9d878ab3-e5ca-4218-aef6-44cbdc90160a")
    tracer.on_llm_start({"name": "test_llm"}, ["foo"], run_id=run_id)

    run = tracer.run_map[str(run_id)]
    usage_metadata = {"input_tokens": 100, "output_tokens": 200, "total_tokens": 300}
    run.outputs = {
        "generations": [
            [
                {
                    "text": "Hello!",
                    "message": {"content": "Hello!", "usage_metadata": usage_metadata},
                }
            ]
        ]
    }

    captured_run = None

    def capture_run(r: Run) -> None:
        nonlocal captured_run
        captured_run = r

    with unittest.mock.patch.object(tracer, "_update_run_single", capture_run):
        tracer._on_llm_end(run)

    assert captured_run is not None
    assert "metadata" in captured_run.extra
    assert captured_run.extra["metadata"]["usage_metadata"] == usage_metadata

Domain

Subdomains

Frequently Asked Questions

What does test_on_llm_end_stores_usage_metadata_in_run_extra() do?
test_on_llm_end_stores_usage_metadata_in_run_extra() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/tracers/test_langchain.py.
Where is test_on_llm_end_stores_usage_metadata_in_run_extra() defined?
test_on_llm_end_stores_usage_metadata_in_run_extra() is defined in libs/core/tests/unit_tests/tracers/test_langchain.py at line 252.

Analyze Your Own Codebase

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

Try Supermodel Free