Skip to content

Conversation

MuhammadSaqib786
Copy link

Summary

Added a short section in context.md explaining how to use ToolContext, which extends RunContextWrapper and gives access to tool metadata such as tool_name, tool_call_id, and tool_arguments.

Why

This helps developers understand when to use ToolContext for advanced cases, without replacing the existing RunContextWrapper examples.

Changes

  • Added a new "Advanced: ToolContext" section under Local Context.
  • Included a simple example with explanations.

Notes

This PR updates only the documentation. No code or API changes were made.

Thanks for reviewing!

@seratch seratch added the documentation Improvements or additions to documentation label Oct 8, 2025
`ToolContext` provides the same `.context` property as `RunContextWrapper`,
plus additional fields specific to the current tool call:

- `tool_name` – the name of the tool being invoked

This comment was marked as off-topic.

Copy link
Author

@MuhammadSaqib786 MuhammadSaqib786 Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing! Please let me know if there’s anything you’d like me to adjust or clarify.

Copy link
Author

@MuhammadSaqib786 MuhammadSaqib786 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing! Please let me know if there’s anything you’d like me to adjust or clarify.

uid: int

@function_tool
async def fetch_user_age(ctx: ToolContext[UserInfo]) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the context object must be the first argument when you receive typed parameters. can you use this weather example instead?

from typing import Annotated

from pydantic import BaseModel, Field

from agents import Agent, Runner, function_tool
from agents.tool_context import ToolContext

class WeatherContext(BaseModel):
    user_id: str

class Weather(BaseModel):
    city: str = Field(description="The city name")
    temperature_range: str = Field(description="The temperature range in Celsius")
    conditions: str = Field(description="The weather conditions")

@function_tool
def get_weather(ctx: ToolContext[WeatherContext], city: Annotated[str, "The city to get the weather for"]) -> Weather:
    print(f"[debug] Tool context: (name: {ctx.tool_name}, call_id: {ctx.tool_call_id}, args: {ctx.tool_arguments})")
    return Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind.")

agent = Agent(
    name="Weather Agent",
    instructions="You are a helpful agent that can tell the weather of a given city.",
    tools=[get_weather],
)

@seratch seratch removed the request for review from khamsukanon79120 October 10, 2025 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants