Home / Function/ on_llm_start() — langchain Function Reference

on_llm_start() — langchain Function Reference

Architecture documentation for the on_llm_start() function in manager.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  aff29c00_eb3e_fd6a_1d26_af2c7296121e["on_llm_start()"]
  90ea8982_7d84_5e8f_4b5a_533f89fc7211["AsyncCallbackManager"]
  aff29c00_eb3e_fd6a_1d26_af2c7296121e -->|defined in| 90ea8982_7d84_5e8f_4b5a_533f89fc7211
  082e343b_042a_f645_ef5e_35d66aa78bc3["on_llm_start()"]
  082e343b_042a_f645_ef5e_35d66aa78bc3 -->|calls| aff29c00_eb3e_fd6a_1d26_af2c7296121e
  082e343b_042a_f645_ef5e_35d66aa78bc3["on_llm_start()"]
  aff29c00_eb3e_fd6a_1d26_af2c7296121e -->|calls| 082e343b_042a_f645_ef5e_35d66aa78bc3
  3a243d21_72d1_dca9_f7be_acbb482b9f86["ahandle_event()"]
  aff29c00_eb3e_fd6a_1d26_af2c7296121e -->|calls| 3a243d21_72d1_dca9_f7be_acbb482b9f86
  style aff29c00_eb3e_fd6a_1d26_af2c7296121e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/callbacks/manager.py lines 1785–1871

    async def on_llm_start(
        self,
        serialized: dict[str, Any],
        prompts: list[str],
        run_id: UUID | None = None,
        **kwargs: Any,
    ) -> list[AsyncCallbackManagerForLLMRun]:
        """Run when LLM starts running.

        Args:
            serialized: The serialized LLM.
            prompts: The list of prompts.
            run_id: The ID of the run.
            **kwargs: Additional keyword arguments.

        Returns:
            The list of async callback managers, one for each LLM run corresponding to
            each prompt.
        """
        inline_tasks = []
        non_inline_tasks = []
        inline_handlers = [handler for handler in self.handlers if handler.run_inline]
        non_inline_handlers = [
            handler for handler in self.handlers if not handler.run_inline
        ]
        managers = []

        for prompt in prompts:
            if run_id is not None:
                run_id_ = run_id
                run_id = None
            else:
                run_id_ = uuid7()

            if inline_handlers:
                inline_tasks.append(
                    ahandle_event(
                        inline_handlers,
                        "on_llm_start",
                        "ignore_llm",
                        serialized,
                        [prompt],
                        run_id=run_id_,
                        parent_run_id=self.parent_run_id,
                        tags=self.tags,
                        metadata=self.metadata,
                        **kwargs,
                    )
                )
            else:
                non_inline_tasks.append(
                    ahandle_event(
                        non_inline_handlers,
                        "on_llm_start",
                        "ignore_llm",
                        serialized,
                        [prompt],
                        run_id=run_id_,
                        parent_run_id=self.parent_run_id,
                        tags=self.tags,
                        metadata=self.metadata,
                        **kwargs,
                    )
                )

            managers.append(
                AsyncCallbackManagerForLLMRun(
                    run_id=run_id_,
                    handlers=self.handlers,
                    inheritable_handlers=self.inheritable_handlers,
                    parent_run_id=self.parent_run_id,
                    tags=self.tags,
                    inheritable_tags=self.inheritable_tags,
                    metadata=self.metadata,
                    inheritable_metadata=self.inheritable_metadata,
                )
            )

        # Run inline tasks sequentially
        for inline_task in inline_tasks:
            await inline_task

Domain

Subdomains

Called By

Frequently Asked Questions

What does on_llm_start() do?
on_llm_start() is a function in the langchain codebase, defined in libs/core/langchain_core/callbacks/manager.py.
Where is on_llm_start() defined?
on_llm_start() is defined in libs/core/langchain_core/callbacks/manager.py at line 1785.
What does on_llm_start() call?
on_llm_start() calls 2 function(s): ahandle_event, on_llm_start.
What calls on_llm_start()?
on_llm_start() is called by 1 function(s): on_llm_start.

Analyze Your Own Codebase

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

Try Supermodel Free