Home / Function/ initialize_agent() — langchain Function Reference

initialize_agent() — langchain Function Reference

Architecture documentation for the initialize_agent() function in initialize.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a2727bf3_a0d7_be36_ef34_a9e5955c960d["initialize_agent()"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b["initialize.py"]
  a2727bf3_a0d7_be36_ef34_a9e5955c960d -->|defined in| dc73acf6_5759_fd39_4791_4836f8ca0d6b
  style a2727bf3_a0d7_be36_ef34_a9e5955c960d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/initialize.py lines 24–116

def initialize_agent(
    tools: Sequence[BaseTool],
    llm: BaseLanguageModel,
    agent: AgentType | None = None,
    callback_manager: BaseCallbackManager | None = None,
    agent_path: str | None = None,
    agent_kwargs: dict | None = None,
    *,
    tags: Sequence[str] | None = None,
    **kwargs: Any,
) -> AgentExecutor:
    """Load an agent executor given tools and LLM.

    !!! warning

        This function is no deprecated in favor of
        [`create_agent`][langchain.agents.create_agent] from the `langchain`
        package, which provides a more flexible agent factory with middleware
        support, structured output, and integration with LangGraph.

        For migration guidance, see
        [Migrating to langchain v1](https://docs.langchain.com/oss/python/migrate/langchain-v1)
        and
        [Migrating from AgentExecutor](https://python.langchain.com/docs/how_to/migrate_agent/).

    Args:
        tools: List of tools this agent has access to.
        llm: Language model to use as the agent.
        agent: Agent type to use. If `None` and agent_path is also None, will default
            to AgentType.ZERO_SHOT_REACT_DESCRIPTION.
        callback_manager: CallbackManager to use. Global callback manager is used if
            not provided.
        agent_path: Path to serialized agent to use. If `None` and agent is also None,
            will default to AgentType.ZERO_SHOT_REACT_DESCRIPTION.
        agent_kwargs: Additional keyword arguments to pass to the underlying agent.
        tags: Tags to apply to the traced runs.
        kwargs: Additional keyword arguments passed to the agent executor.

    Returns:
        An agent executor.

    Raises:
        ValueError: If both `agent` and `agent_path` are specified.
        ValueError: If `agent` is not a valid agent type.
        ValueError: If both `agent` and `agent_path` are None.
    """
    tags_ = list(tags) if tags else []
    if agent is None and agent_path is None:
        agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION
    if agent is not None and agent_path is not None:
        msg = (
            "Both `agent` and `agent_path` are specified, "
            "but at most only one should be."
        )
        raise ValueError(msg)
    if agent is not None:
        if agent not in AGENT_TO_CLASS:
            msg = (
                f"Got unknown agent type: {agent}. "
                f"Valid types are: {AGENT_TO_CLASS.keys()}."
            )
            raise ValueError(msg)
        tags_.append(agent.value if isinstance(agent, AgentType) else agent)
        agent_cls = AGENT_TO_CLASS[agent]
        agent_kwargs = agent_kwargs or {}
        agent_obj = agent_cls.from_llm_and_tools(
            llm,
            tools,
            callback_manager=callback_manager,
            **agent_kwargs,
        )
    elif agent_path is not None:
        agent_obj = load_agent(
            agent_path,
            llm=llm,
            tools=tools,
            callback_manager=callback_manager,
        )
        with contextlib.suppress(NotImplementedError):
            # TODO: Add tags from the serialized object directly.
            tags_.append(agent_obj._agent_type)  # noqa: SLF001

Subdomains

Frequently Asked Questions

What does initialize_agent() do?
initialize_agent() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/initialize.py.
Where is initialize_agent() defined?
initialize_agent() is defined in libs/langchain/langchain_classic/agents/initialize.py at line 24.

Analyze Your Own Codebase

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

Try Supermodel Free