Home / Function/ test_astream_fallback_to_ainvoke() — langchain Function Reference

test_astream_fallback_to_ainvoke() — langchain Function Reference

Architecture documentation for the test_astream_fallback_to_ainvoke() function in test_base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  54f6ee0b_3c1d_8c46_fa07_aa95e36da952["test_astream_fallback_to_ainvoke()"]
  8830054d_ac1e_daa9_c6c5_ff55b10d0bf3["test_base.py"]
  54f6ee0b_3c1d_8c46_fa07_aa95e36da952 -->|defined in| 8830054d_ac1e_daa9_c6c5_ff55b10d0bf3
  style 54f6ee0b_3c1d_8c46_fa07_aa95e36da952 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/language_models/chat_models/test_base.py lines 183–214

async def test_astream_fallback_to_ainvoke() -> None:
    """Test `astream()` uses appropriate implementation."""

    class ModelWithGenerate(BaseChatModel):
        @override
        def _generate(
            self,
            messages: list[BaseMessage],
            stop: list[str] | None = None,
            run_manager: CallbackManagerForLLMRun | None = None,
            **kwargs: Any,
        ) -> ChatResult:
            """Top Level call."""
            message = AIMessage(content="hello")
            generation = ChatGeneration(message=message)
            return ChatResult(generations=[generation])

        @property
        def _llm_type(self) -> str:
            return "fake-chat-model"

    model = ModelWithGenerate()
    chunks = list(model.stream("anything"))
    # BaseChatModel.stream is typed to return Iterator[BaseMessageChunk].
    # When streaming is disabled, it returns Iterator[BaseMessage], so the type hint
    # is not strictly correct.
    # LangChain documents a pattern of adding BaseMessageChunks to accumulate a stream.
    # This may be better done with `reduce(operator.add, chunks)`.
    assert chunks == [_any_id_ai_message(content="hello")]

    chunks = [chunk async for chunk in model.astream("anything")]
    assert chunks == [_any_id_ai_message(content="hello")]

Subdomains

Frequently Asked Questions

What does test_astream_fallback_to_ainvoke() do?
test_astream_fallback_to_ainvoke() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/language_models/chat_models/test_base.py.
Where is test_astream_fallback_to_ainvoke() defined?
test_astream_fallback_to_ainvoke() is defined in libs/core/tests/unit_tests/language_models/chat_models/test_base.py at line 183.

Analyze Your Own Codebase

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

Try Supermodel Free