Home / File/ test_async_callbacks.py — langchain Source File

test_async_callbacks.py — langchain Source File

Architecture documentation for test_async_callbacks.py, a python file in the langchain codebase. 11 imports, 0 dependents.

File python CoreAbstractions Serialization 11 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9["test_async_callbacks.py"]
  a327e534_84f6_5308_58ca_5727d5eda0cf["asyncio"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> a327e534_84f6_5308_58ca_5727d5eda0cf
  436f77bc_653d_0edb_555c_c2679d5a59ac["itertools"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> 436f77bc_653d_0edb_555c_c2679d5a59ac
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  8dfa0cac_d802_3ccd_f710_43a5e70da3a5["uuid"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> 8dfa0cac_d802_3ccd_f710_43a5e70da3a5
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  e85cc09a_55c8_b775_4454_f3a6771761b9["pytest_benchmark.fixture"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> e85cc09a_55c8_b775_4454_f3a6771761b9
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> 91721f45_4909_e489_8c1f_084f8bd87145
  7e64d143_ea36_1c73_4897_1d0ae1757b5b["langchain_core.callbacks.base"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> 7e64d143_ea36_1c73_4897_1d0ae1757b5b
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> d758344f_537f_649e_f467_b9d7442e86df
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  ae1a491c_0c3b_8766_56f6_b65915d5a4b9 --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  style ae1a491c_0c3b_8766_56f6_b65915d5a4b9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import asyncio
from itertools import cycle
from typing import Any
from uuid import UUID

import pytest
from pytest_benchmark.fixture import BenchmarkFixture
from typing_extensions import override

from langchain_core.callbacks.base import AsyncCallbackHandler
from langchain_core.language_models import GenericFakeChatModel
from langchain_core.messages import AIMessage, BaseMessage
from langchain_core.outputs import ChatGenerationChunk, GenerationChunk


class MyCustomAsyncHandler(AsyncCallbackHandler):
    @override
    async def on_chat_model_start(
        self,
        serialized: dict[str, Any],
        messages: list[list[BaseMessage]],
        *,
        run_id: UUID,
        parent_run_id: UUID | None = None,
        tags: list[str] | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> Any:
        # Do nothing
        # Required to implement since this is an abstract method
        pass

    @override
    async def on_llm_new_token(
        self,
        token: str,
        *,
        chunk: GenerationChunk | ChatGenerationChunk | None = None,
        run_id: UUID,
        parent_run_id: UUID | None = None,
        tags: list[str] | None = None,
        **kwargs: Any,
    ) -> None:
        await asyncio.sleep(0)


@pytest.mark.benchmark
async def test_async_callbacks_in_sync(benchmark: BenchmarkFixture) -> None:
    infinite_cycle = cycle([AIMessage(content=" ".join(["hello", "goodbye"] * 5))])
    model = GenericFakeChatModel(messages=infinite_cycle)

    @benchmark  # type: ignore[untyped-decorator]
    def sync_callbacks() -> None:
        for _ in range(5):
            for _ in model.stream("meow", {"callbacks": [MyCustomAsyncHandler()]}):
                pass

Subdomains

Dependencies

  • asyncio
  • itertools
  • langchain_core.callbacks.base
  • langchain_core.language_models
  • langchain_core.messages
  • langchain_core.outputs
  • pytest
  • pytest_benchmark.fixture
  • typing
  • typing_extensions
  • uuid

Frequently Asked Questions

What does test_async_callbacks.py do?
test_async_callbacks.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in test_async_callbacks.py?
test_async_callbacks.py defines 1 function(s): test_async_callbacks_in_sync.
What does test_async_callbacks.py depend on?
test_async_callbacks.py imports 11 module(s): asyncio, itertools, langchain_core.callbacks.base, langchain_core.language_models, langchain_core.messages, langchain_core.outputs, pytest, pytest_benchmark.fixture, and 3 more.
Where is test_async_callbacks.py in the architecture?
test_async_callbacks.py is located at libs/core/tests/benchmarks/test_async_callbacks.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/tests/benchmarks).

Analyze Your Own Codebase

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

Try Supermodel Free