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 fec5d52c_3ecd_766f_4c33_14f832ac0251["test_astream()"] f27640dd_3870_5548_d153_f9504ae1021f["test_chat_models.py"] fec5d52c_3ecd_766f_4c33_14f832ac0251 -->|defined in| f27640dd_3870_5548_d153_f9504ae1021f style fec5d52c_3ecd_766f_4c33_14f832ac0251 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/anthropic/tests/integration_tests/test_chat_models.py lines 87–146
async def test_astream() -> None:
"""Test streaming tokens from Anthropic."""
llm = ChatAnthropic(model_name=MODEL_NAME) # type: ignore[call-arg, call-arg]
full: BaseMessageChunk | None = None
chunks_with_input_token_counts = 0
chunks_with_output_token_counts = 0
async for token in llm.astream("I'm Pickle Rick"):
assert isinstance(token.content, str)
full = cast("BaseMessageChunk", token) if full is None else full + token
assert isinstance(token, AIMessageChunk)
if token.usage_metadata is not None:
if token.usage_metadata.get("input_tokens"):
chunks_with_input_token_counts += 1
if token.usage_metadata.get("output_tokens"):
chunks_with_output_token_counts += 1
if chunks_with_input_token_counts != 1 or chunks_with_output_token_counts != 1:
msg = (
"Expected exactly one chunk with input or output token counts. "
"AIMessageChunk aggregation adds counts. Check that "
"this is behaving properly."
)
raise AssertionError(
msg,
)
# check token usage is populated
assert isinstance(full, AIMessageChunk)
assert len(full.content_blocks) == 1
assert full.content_blocks[0]["type"] == "text"
assert full.content_blocks[0]["text"]
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["total_tokens"] > 0
assert (
full.usage_metadata["input_tokens"] + full.usage_metadata["output_tokens"]
== full.usage_metadata["total_tokens"]
)
assert "stop_reason" in full.response_metadata
assert "stop_sequence" in full.response_metadata
# Check expected raw API output
async_client = llm._async_client
params: dict = {
"model": MODEL_NAME,
"max_tokens": 1024,
"messages": [{"role": "user", "content": "hi"}],
"temperature": 0.0,
}
stream = await async_client.messages.create(**params, stream=True)
async for event in stream:
if event.type == "message_start":
assert event.message.usage.input_tokens > 1
# Different models may report different initial output token counts
# in the message_start event. Ensure it's a positive value.
assert event.message.usage.output_tokens >= 1
elif event.type == "message_delta":
assert event.usage.output_tokens >= 1
else:
pass
Domain
Subdomains
Source
Frequently Asked Questions
What does test_astream() do?
test_astream() is a function in the langchain codebase, defined in libs/partners/anthropic/tests/integration_tests/test_chat_models.py.
Where is test_astream() defined?
test_astream() is defined in libs/partners/anthropic/tests/integration_tests/test_chat_models.py at line 87.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free