-
Notifications
You must be signed in to change notification settings - Fork 2.7k
docs: add ToolContext section for advanced tool metadata #1868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
`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.
This comment was marked as off-topic.
Sorry, something went wrong.
There was a problem hiding this 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.
There was a problem hiding this 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: |
There was a problem hiding this comment.
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],
)
Summary
Added a short section in
context.md
explaining how to useToolContext
, which extendsRunContextWrapper
and gives access to tool metadata such astool_name
,tool_call_id
, andtool_arguments
.Why
This helps developers understand when to use
ToolContext
for advanced cases, without replacing the existingRunContextWrapper
examples.Changes
Notes
This PR updates only the documentation. No code or API changes were made.
Thanks for reviewing!