# Class LanggraphAgent (1.153.1)

1.153.1 (latest) 1.153.0 1.152.0 1.151.0 1.150.0 1.149.0 1.148.1 1.147.0 1.146.0 1.145.0 1.144.0 1.143.0 1.142.0 1.141.0 1.140.0 1.139.0 1.138.0 1.137.0 1.136.0 1.135.0 1.134.0 1.133.0 1.132.0 1.131.0 1.130.0 1.129.0 1.122.0 1.121.0 1.120.0 1.119.0 1.118.0 1.117.0 1.95.1 1.94.0 1.93.1 1.92.0 1.91.0 1.90.0 1.89.0 1.88.0 1.87.0 1.86.0 1.85.0 1.84.0 1.83.0 1.82.0 1.81.0 1.80.0 1.79.0 1.78.0 1.77.0 1.76.0 1.75.0 1.74.0 1.73.0 1.72.0 1.71.1 1.70.0 1.69.0 1.68.0 1.67.1 1.66.0 1.65.0 1.63.0 1.62.0 1.60.0 1.59.0

    LanggraphAgent(
        model: str,
        *,
        tools: typing.Optional[typing.Sequence[_ToolLike]] = None,
        model_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        model_tool_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        model_builder: typing.Optional[typing.Callable[[...], BaseLanguageModel]] = None,
        runnable_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        runnable_builder: typing.Optional[typing.Callable[[...], typing.Any]] = None,
        checkpointer_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        checkpointer_builder: typing.Optional[
            typing.Callable[[...], BaseCheckpointSaver]
        ] = None,
        enable_tracing: bool = False,
        instrumentor_builder: typing.Optional[typing.Callable[[...], typing.Any]] = None
    )

A LangGraph Agent.

## Methods

### LanggraphAgent

    LanggraphAgent(
        model: str,
        *,
        tools: typing.Optional[typing.Sequence[_ToolLike]] = None,
        model_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        model_tool_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        model_builder: typing.Optional[typing.Callable[[...], BaseLanguageModel]] = None,
        runnable_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        runnable_builder: typing.Optional[typing.Callable[[...], typing.Any]] = None,
        checkpointer_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
        checkpointer_builder: typing.Optional[
            typing.Callable[[...], BaseCheckpointSaver]
        ] = None,
        enable_tracing: bool = False,
        instrumentor_builder: typing.Optional[typing.Callable[[...], typing.Any]] = None
    )

Initializes the LangGraph Agent.

Under-the-hood, assuming .set_up() is called, this will correspond to

    model = model_builder(model_name=model, model_kwargs=model_kwargs)
    runnable = runnable_builder(
        model=model,
        tools=tools,
        model_tool_kwargs=model_tool_kwargs,
        runnable_kwargs=runnable_kwargs,
    )

When everything is based on their default values, this corresponds to

    # model_builder
    from langchain_google_vertexai import ChatVertexAI
    llm = ChatVertexAI(model_name=model, **model_kwargs)

    # runnable_builder
    from langgraph.prebuilt import create_react_agent
    llm_with_tools = llm.bind_tools(tools=tools, **model_tool_kwargs)
    runnable = create_react_agent(
        llm_with_tools,
        tools=tools,
        **runnable_kwargs,
    )

By default, no checkpointer is used (i.e. there is no state history). To
enable checkpointing, provide a `checkpointer_builder` function that
returns a checkpointer instance.

**Example using Spanner:**

    def checkpointer_builder(instance_id, database_id, project_id, **kwargs):
        from langchain_google_spanner import SpannerCheckpointSaver

        checkpointer = SpannerCheckpointSaver(instance_id, database_id, project_id)
        with checkpointer.cursor() as cur:
            cur.execute("DROP TABLE IF EXISTS checkpoints")
            cur.execute("DROP TABLE IF EXISTS checkpoint_writes")
        checkpointer.setup()

        return checkpointer

**Example using an in-memory checkpointer:**

    def checkpointer_builder(**kwargs):
        from langgraph.checkpoint.memory import MemorySaver

        return MemorySaver()

The `checkpointer_builder` function will be called with any keyword
arguments passed to the agent's constructor. Ensure your
`checkpointer_builder` function accepts `**kwargs` to handle these
arguments, even if unused.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `model` | `str` Optional. The name of the model (e.g. "gemini-1.0-pro"). |
| `tools` | `Sequence[langchain_core.tools.BaseTool, Callable]` Optional. The tools for the agent to be able to use. All input callables (e.g. function or class method) will be converted to a langchain.tools.base.StructuredTool. Defaults to None. |
| `model_kwargs` | `Mapping[str, Any]` Optional. Additional keyword arguments for the constructor of chat_models.ChatVertexAI. An example would be ``` { # temperature (float): Sampling temperature, it controls the # degree of randomness in token selection. "temperature": 0.28, # max_output_tokens (int): Token limit determines the # maximum amount of text output from one prompt. "max_output_tokens": 1000, # top_p (float): Tokens are selected from most probable to # least, until the sum of their probabilities equals the # top_p value. "top_p": 0.95, # top_k (int): How the model selects tokens for output, the # next token is selected from among the top_k most probable # tokens. "top_k": 40, } ``` <br /> |
| `model_tool_kwargs` | `Mapping[str, Any]` Optional. Additional keyword arguments when binding tools to the model using `model.bind_tools()`. |
| `model_builder` | `Callable[..., BaseLanguageModel]` Optional. Callable that returns a new language model. Defaults to a a callable that returns ChatVertexAI based on `model`, `model_kwargs` and the parameters in `https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai`. |
| `runnable_kwargs` | `Mapping[str, Any]` Optional. Additional keyword arguments for the constructor of langchain.runnables.history.RunnableWithMessageHistory if chat_history is specified. If chat_history is None, this will be ignored. |
| `runnable_builder` | `Callable[..., RunnableSerializable]` Optional. Callable that returns a new runnable. This can be used for customizing the orchestration logic of the Agent based on the model returned by `model_builder` and the rest of the input arguments. |
| `checkpointer_kwargs` | `Mapping[str, Any]` Optional. Additional keyword arguments for the constructor of the checkpointer returned by `checkpointer_builder`. |
| `checkpointer_builder` | `Callable[..., "BaseCheckpointSaver"]` Optional. Callable that returns a checkpointer. This can be used for defining the checkpointer of the Agent. Defaults to None. |
| `enable_tracing` | `bool` Optional. Whether to enable tracing in Cloud Trace. Defaults to False. |
| `instrumentor_builder` | `Callable[..., Any]` Optional. Callable that returns a new instrumentor. This can be used for customizing the instrumentation logic of the Agent. If not provided, a default instrumentor builder will be used. This parameter is ignored if `enable_tracing` is False. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `TypeError` | If there is an invalid tool (e.g. function with an input |
| `tha` | did not specify its type).: |

### clone

    clone() -> vertexai.agent_engines.templates.langgraph.LanggraphAgent

Returns a clone of the LanggraphAgent.

### get_state

    get_state(
        config: typing.Optional[dict[str, typing.Any]] = None, **kwargs: typing.Any
    ) -> typing.Dict[str, typing.Any]

Gets the current state of the Agent.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `config` | `Optional[RunnableConfig]` Optional. The config for invoking the Agent. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `Dict[str, Any]` | The current state of the Agent. |

### get_state_history

    get_state_history(
        config: typing.Optional[dict[str, typing.Any]] = None, **kwargs: typing.Any
    ) -> typing.Iterable[typing.Any]

Gets the state history of the Agent.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `config` | `Optional[RunnableConfig]` Optional. The config for invoking the Agent. |

### query

    query(
        *,
        input: typing.Union[str, typing.Mapping[str, typing.Any]],
        config: typing.Optional[dict[str, typing.Any]] = None,
        **kwargs: typing.Any
    ) -> typing.Dict[str, typing.Any]

Queries the Agent with the given input and config.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `input` | `Union[str, Mapping[str, Any]]` Required. The input to be passed to the Agent. |
| `config` | `langchain_core.runnables.RunnableConfig` Optional. The config (if any) to be used for invoking the Agent. |

### register_operations

    register_operations() -> typing.Mapping[str, typing.Sequence[str]]

Registers the operations of the Agent.

This mapping defines how different operation modes (e.g., "", "stream")
are implemented by specific methods of the Agent. The "default" mode,
represented by the empty string `, is associated with the`query`API,
while the "stream" mode is associated with the`stream_query\` API.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `Mapping[str, Sequence[str]]` | A mapping of operation modes to a list of method names that implement those operation modes. |

### set_up

    set_up()

Sets up the agent for execution of queries at runtime.

It initializes the model, binds the model with tools, and connects it
with the prompt template and output parser.

This method should not be called for an object that being passed to
the ReasoningEngine service for deployment, as it initializes clients
that can not be serialized.

### stream_query

    stream_query(
        *,
        input: typing.Union[str, typing.Mapping[str, typing.Any]],
        config: typing.Optional[dict[str, typing.Any]] = None,
        **kwargs
    ) -> typing.Iterable[typing.Any]

Stream queries the Agent with the given input and config.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `input` | `Union[str, Mapping[str, Any]]` Required. The input to be passed to the Agent. |
| `config` | `langchain_core.runnables.RunnableConfig` Optional. The config (if any) to be used for invoking the Agent. |

### update_state

    update_state(
        config: typing.Optional[dict[str, typing.Any]] = None, **kwargs: typing.Any
    ) -> typing.Dict[str, typing.Any]

Updates the state of the Agent.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `config` | `Optional[RunnableConfig]` Optional. The config for invoking the Agent. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `Dict[str, Any]` | The updated state of the Agent. |