Home / Function/ astream_events() — langchain Function Reference

astream_events() — langchain Function Reference

Architecture documentation for the astream_events() function in base.py from the langchain codebase.

Function python LangChainCore Runnables calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  67cad9a2_ee89_d44e_40ab_e55a36396c0d["astream_events()"]
  4a62481c_02cb_a5de_1833_50669d5351a6["Runnable"]
  67cad9a2_ee89_d44e_40ab_e55a36396c0d -->|defined in| 4a62481c_02cb_a5de_1833_50669d5351a6
  587028d9_69c4_7ed8_45fe_d86e3f085696["astream_events()"]
  587028d9_69c4_7ed8_45fe_d86e3f085696 -->|calls| 67cad9a2_ee89_d44e_40ab_e55a36396c0d
  587028d9_69c4_7ed8_45fe_d86e3f085696["astream_events()"]
  67cad9a2_ee89_d44e_40ab_e55a36396c0d -->|calls| 587028d9_69c4_7ed8_45fe_d86e3f085696
  e2bea970_c5e9_9585_6fa1_e85a5a79ce5e["with_config()"]
  67cad9a2_ee89_d44e_40ab_e55a36396c0d -->|calls| e2bea970_c5e9_9585_6fa1_e85a5a79ce5e
  style 67cad9a2_ee89_d44e_40ab_e55a36396c0d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 1273–1517

    async def astream_events(
        self,
        input: Any,
        config: RunnableConfig | None = None,
        *,
        version: Literal["v1", "v2"] = "v2",
        include_names: Sequence[str] | None = None,
        include_types: Sequence[str] | None = None,
        include_tags: Sequence[str] | None = None,
        exclude_names: Sequence[str] | None = None,
        exclude_types: Sequence[str] | None = None,
        exclude_tags: Sequence[str] | None = None,
        **kwargs: Any,
    ) -> AsyncIterator[StreamEvent]:
        """Generate a stream of events.

        Use to create an iterator over `StreamEvent` that provide real-time information
        about the progress of the `Runnable`, including `StreamEvent` from intermediate
        results.

        A `StreamEvent` is a dictionary with the following schema:

        - `event`: Event names are of the format:
            `on_[runnable_type]_(start|stream|end)`.
        - `name`: The name of the `Runnable` that generated the event.
        - `run_id`: Randomly generated ID associated with the given execution of the
            `Runnable` that emitted the event. A child `Runnable` that gets invoked as
            part of the execution of a parent `Runnable` is assigned its own unique ID.
        - `parent_ids`: The IDs of the parent runnables that generated the event. The
            root `Runnable` will have an empty list. The order of the parent IDs is from
            the root to the immediate parent. Only available for v2 version of the API.
            The v1 version of the API will return an empty list.
        - `tags`: The tags of the `Runnable` that generated the event.
        - `metadata`: The metadata of the `Runnable` that generated the event.
        - `data`: The data associated with the event. The contents of this field
            depend on the type of event. See the table below for more details.

        Below is a table that illustrates some events that might be emitted by various
        chains. Metadata fields have been omitted from the table for brevity.
        Chain definitions have been included after the table.

        !!! note
            This reference table is for the v2 version of the schema.

        | event                  | name                 | chunk                               | input                                             | output                                              |
        | ---------------------- | -------------------- | ----------------------------------- | ------------------------------------------------- | --------------------------------------------------- |
        | `on_chat_model_start`  | `'[model name]'`     |                                     | `{"messages": [[SystemMessage, HumanMessage]]}`   |                                                     |
        | `on_chat_model_stream` | `'[model name]'`     | `AIMessageChunk(content="hello")`   |                                                   |                                                     |
        | `on_chat_model_end`    | `'[model name]'`     |                                     | `{"messages": [[SystemMessage, HumanMessage]]}`   | `AIMessageChunk(content="hello world")`             |
        | `on_llm_start`         | `'[model name]'`     |                                     | `{'input': 'hello'}`                              |                                                     |
        | `on_llm_stream`        | `'[model name]'`     | `'Hello' `                          |                                                   |                                                     |
        | `on_llm_end`           | `'[model name]'`     |                                     | `'Hello human!'`                                  |                                                     |
        | `on_chain_start`       | `'format_docs'`      |                                     |                                                   |                                                     |
        | `on_chain_stream`      | `'format_docs'`      | `'hello world!, goodbye world!'`    |                                                   |                                                     |
        | `on_chain_end`         | `'format_docs'`      |                                     | `[Document(...)]`                                 | `'hello world!, goodbye world!'`                    |
        | `on_tool_start`        | `'some_tool'`        |                                     | `{"x": 1, "y": "2"}`                              |                                                     |
        | `on_tool_end`          | `'some_tool'`        |                                     |                                                   | `{"x": 1, "y": "2"}`                                |
        | `on_retriever_start`   | `'[retriever name]'` |                                     | `{"query": "hello"}`                              |                                                     |
        | `on_retriever_end`     | `'[retriever name]'` |                                     | `{"query": "hello"}`                              | `[Document(...), ..]`                               |
        | `on_prompt_start`      | `'[template_name]'`  |                                     | `{"question": "hello"}`                           |                                                     |
        | `on_prompt_end`        | `'[template_name]'`  |                                     | `{"question": "hello"}`                           | `ChatPromptValue(messages: [SystemMessage, ...])`   |

        In addition to the standard events, users can also dispatch custom events (see example below).

        Custom events will be only be surfaced with in the v2 version of the API!

        A custom event has following format:

        | Attribute   | Type   | Description                                                                                               |
        | ----------- | ------ | --------------------------------------------------------------------------------------------------------- |
        | `name`      | `str`  | A user defined name for the event.                                                                        |
        | `data`      | `Any`  | The data associated with the event. This can be anything, though we suggest making it JSON serializable.  |

        Here are declarations associated with the standard events shown above:

        `format_docs`:

        ```python
        def format_docs(docs: list[Document]) -> str:
            '''Format the docs.'''
            return ", ".join([doc.page_content for doc in docs])

Domain

Subdomains

Called By

Frequently Asked Questions

What does astream_events() do?
astream_events() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is astream_events() defined?
astream_events() is defined in libs/core/langchain_core/runnables/base.py at line 1273.
What does astream_events() call?
astream_events() calls 2 function(s): astream_events, with_config.
What calls astream_events()?
astream_events() is called by 1 function(s): astream_events.

Analyze Your Own Codebase

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

Try Supermodel Free