Home / Function/ test_astream() — langchain Function Reference

test_astream() — langchain Function Reference

Architecture documentation for the test_astream() function in test_chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  88fae6da_ae13_4851_41f3_7256c6701ae9["test_astream()"]
  9003580b_9bcf_c959_a278_ba1963301452["test_chat_models.py"]
  88fae6da_ae13_4851_41f3_7256c6701ae9 -->|defined in| 9003580b_9bcf_c959_a278_ba1963301452
  style 88fae6da_ae13_4851_41f3_7256c6701ae9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/mistralai/tests/integration_tests/test_chat_models.py lines 18–51

async def test_astream() -> None:
    """Test streaming tokens from ChatMistralAI."""
    llm = ChatMistralAI()

    full: BaseMessageChunk | None = None
    chunks_with_token_counts = 0
    chunks_with_response_metadata = 0
    async for token in llm.astream("Hello"):
        assert isinstance(token, AIMessageChunk)
        assert isinstance(token.content, str)
        full = token if full is None else full + token
        if token.usage_metadata is not None:
            chunks_with_token_counts += 1
        if token.response_metadata and not set(token.response_metadata.keys()).issubset(
            {"model_provider", "output_version"}
        ):
            chunks_with_response_metadata += 1
    if chunks_with_token_counts != 1 or chunks_with_response_metadata != 1:
        msg = (
            "Expected exactly one chunk with token counts or response_metadata. "
            "AIMessageChunk aggregation adds / appends counts and metadata. Check that "
            "this is behaving properly."
        )
        raise AssertionError(msg)
    assert isinstance(full, AIMessageChunk)
    assert full.usage_metadata is not None
    assert full.usage_metadata["input_tokens"] > 0
    assert full.usage_metadata["output_tokens"] > 0
    assert (
        full.usage_metadata["input_tokens"] + full.usage_metadata["output_tokens"]
        == full.usage_metadata["total_tokens"]
    )
    assert isinstance(full.response_metadata["model_name"], str)
    assert full.response_metadata["model_name"]

Domain

Subdomains

Frequently Asked Questions

What does test_astream() do?
test_astream() is a function in the langchain codebase, defined in libs/partners/mistralai/tests/integration_tests/test_chat_models.py.
Where is test_astream() defined?
test_astream() is defined in libs/partners/mistralai/tests/integration_tests/test_chat_models.py at line 18.

Analyze Your Own Codebase

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

Try Supermodel Free