Skip to content

OpenAI tool context #942

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

Merged
merged 5 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion temporalio/contrib/openai_agents/temporal_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def activity_as_tool(
This function takes a Temporal activity function and converts it into an
OpenAI agent tool that can be used by the agent to execute the activity
during workflow execution. The tool will automatically handle the conversion
of inputs and outputs between the agent and the activity.
of inputs and outputs between the agent and the activity. Note that if you take a context,
mutation will not be persisted, as the activity may not be running in the same location.

Args:
fn: A Temporal activity function to convert to a tool.
Expand Down Expand Up @@ -85,6 +86,11 @@ async def run_activity(ctx: RunContextWrapper[Any], input: str) -> Any:

# Activities don't support keyword only arguments, so we can ignore the kwargs_dict return
args, _ = schema.to_call_args(schema.params_pydantic_model(**json_data))

# Add the context to the arguments if it takes that
if schema.takes_context:
args = [ctx] + args

result = await workflow.execute_activity(
fn,
args=args,
Expand Down
46 changes: 43 additions & 3 deletions tests/contrib/openai_agents/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ async def get_weather_object(input: WeatherInput) -> Weather:
)


@activity.defn
async def get_weather_context(ctx: RunContextWrapper[str], city: str) -> Weather:
"""
Get the weather for a given city.
"""
return Weather(city=city, temperature_range="14-20C", conditions=ctx.context)


class TestWeatherModel(TestModel):
responses = [
ModelResponse(
Expand Down Expand Up @@ -265,6 +273,20 @@ class TestWeatherModel(TestModel):
usage=Usage(),
response_id=None,
),
ModelResponse(
output=[
ResponseFunctionToolCall(
arguments='{"city":"Tokyo"}',
call_id="call",
name="get_weather_context",
type="function_call",
id="id",
status="completed",
)
],
usage=Usage(),
response_id=None,
),
ModelResponse(
output=[
ResponseOutputMessage(
Expand Down Expand Up @@ -304,9 +326,14 @@ async def run(self, question: str) -> str:
activity_as_tool(
get_weather_country, start_to_close_timeout=timedelta(seconds=10)
),
activity_as_tool(
get_weather_context, start_to_close_timeout=timedelta(seconds=10)
),
],
) # type: Agent
result = await Runner.run(starting_agent=agent, input=question)
result = await Runner.run(
starting_agent=agent, input=question, context="Stormy"
)
return result.final_output


Expand Down Expand Up @@ -337,6 +364,7 @@ async def test_tool_workflow(client: Client, use_local_model: bool):
get_weather,
get_weather_object,
get_weather_country,
get_weather_context,
],
interceptors=[OpenAIAgentsTracingInterceptor()],
) as worker:
Expand All @@ -357,7 +385,7 @@ async def test_tool_workflow(client: Client, use_local_model: bool):
if e.HasField("activity_task_completed_event_attributes"):
events.append(e)

assert len(events) == 7
assert len(events) == 9
assert (
"function_call"
in events[0]
Expand Down Expand Up @@ -395,11 +423,23 @@ async def test_tool_workflow(client: Client, use_local_model: bool):
.data.decode()
)
assert (
"Test weather result"
"function_call"
in events[6]
.activity_task_completed_event_attributes.result.payloads[0]
.data.decode()
)
assert (
"Stormy"
in events[7]
.activity_task_completed_event_attributes.result.payloads[0]
.data.decode()
)
assert (
"Test weather result"
in events[8]
.activity_task_completed_event_attributes.result.payloads[0]
.data.decode()
)


class TestPlannerModel(OpenAIResponsesModel):
Expand Down