@tool `args_schema` conflicts with `runtime`

In v1, when setting args_schema, the runtime field does not get injected. Is this an intended behavior or a potential bug?

Hi, could you please share an example? Thanks!

Hi @zhanghaili and @sydney-runkle

I’ve been experimenting with the args_schema and this is my observation:

1) Works: no args_schema, runtime injected from signature

from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langchain.tools import ToolRuntime
from langchain.agents import create_agent
from langchain_core.messages import HumanMessage

load_dotenv(verbose=True)

@tool(description="Add two integers and return the sum as a string.")
def add(a: int, b: int, runtime: ToolRuntime) -> str:
    print(f"Runtime config: {runtime.config}")
    return str(a + b)

agent = create_agent(
    model=ChatAnthropic(model="claude-3-7-sonnet-latest"),
    tools=[add],
    system_prompt="You are helpful; call tools to compute exact answers.",
)

result = agent.invoke({"messages": [HumanMessage("What is 2 + 3?")]})

for msg in result["messages"]:
    msg.pretty_print()

2) Works: args_schema includes runtime (Pydantic)

from typing import AnyStr

from dotenv import load_dotenv
from pydantic import BaseModel, ConfigDict
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langchain.tools import ToolRuntime
from langchain.agents import create_agent
from langchain_core.messages import HumanMessage

load_dotenv(verbose=True)

class AddInput(BaseModel):
    # Allow ToolRuntime (which contains BaseStore, etc.)
    model_config = ConfigDict(arbitrary_types_allowed=True)

    a: int
    b: int
    runtime: ToolRuntime  # injected; hidden from the model

@tool(args_schema=AddInput, description="Add two integers; runtime provided via injection.")
def add2(a: int, b: int, runtime: ToolRuntime) -> str:
    print(f"Runtime config: {runtime.config}")
    return str(a + b)

agent = create_agent(
    model=ChatAnthropic(model="claude-3-7-sonnet-latest"),
    tools=[add2],
    system_prompt="You are helpful; call tools to compute exact answers.",
)

result = agent.invoke({"messages": [HumanMessage("Compute 7 + 8")]})

for msg in result["messages"]:
    msg.pretty_print()

3) Fails: args_schema omits runtime (Pydantic)

from dotenv import load_dotenv
from pydantic import BaseModel, ConfigDict
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langchain.tools import ToolRuntime
from langchain.agents import create_agent
from langchain_core.messages import HumanMessage

load_dotenv(verbose=True)

class AddInputNoRuntime(BaseModel):
    # Allow ToolRuntime (which contains BaseStore, etc.)
    model_config = ConfigDict(arbitrary_types_allowed=True)

    a: int
    b: int

@tool(args_schema=AddInputNoRuntime, description="Add two integers (will fail: runtime not in schema).")
def add3(a: int, b: int, runtime: ToolRuntime) -> str:
    # runtime is required by the function but not in schema → not injected
    return str(a + b)

agent = create_agent(
    model=ChatAnthropic(model="claude-3-7-sonnet-latest"),
    tools=[add3],
    system_prompt="You are helpful; call tools to compute exact answers.",
)

# Expected to raise because runtime won't be injected
result = agent.invoke({"messages": [HumanMessage("Add 10 and 20")]})

for msg in result["messages"]:
    msg.pretty_print()

Is point 3 your case @zhanghaili ?

Thanks for pointing that out, I’m in scenario 3! :folded_hands:

1 Like

Hi @TheSecondStep

Thank you for your message. Could you please create separate post for this? And it would be easier to get helped when you format post correctly - right now it is so hard to read and understand.

Hi, This is the new post link:

hi @zhanghaili

I think because of the further messages, this post is no longer marked as solved. Could you mark it again please? :slight_smile: