Skip to content

Commit 234606f

Browse files
authored
fix: Properly deserialize complex tool arguments with Temporal (#2686)
1 parent ed65fb5 commit 234606f

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_function_toolset.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ async def call_tool_activity(params: _CallToolParams, deps: AgentDepsT) -> Any:
5151
'Removing or renaming tools during an agent run is not supported with Temporal.'
5252
) from e
5353

54-
return await self.wrapped.call_tool(name, params.tool_args, ctx, tool)
54+
# The tool args will already have been validated into their proper types in the `ToolManager`,
55+
# but `execute_activity` would have turned them into simple Python types again, so we need to re-validate them.
56+
args_dict = tool.args_validator.validate_python(params.tool_args)
57+
return await self.wrapped.call_tool(name, args_dict, ctx, tool)
5558

5659
# Set type hint explicitly so that Temporal can take care of serialization and deserialization
5760
call_tool_activity.__annotations__['deps'] = deps_type
@@ -85,7 +88,11 @@ async def call_tool(
8588
return await workflow.execute_activity( # pyright: ignore[reportUnknownMemberType]
8689
activity=self.call_tool_activity,
8790
args=[
88-
_CallToolParams(name=name, tool_args=tool_args, serialized_run_context=serialized_run_context),
91+
_CallToolParams(
92+
name=name,
93+
tool_args=tool_args,
94+
serialized_run_context=serialized_run_context,
95+
),
8996
ctx.deps,
9097
],
9198
**tool_activity_config,

tests/test_temporal.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,15 @@ async def get_country(ctx: RunContext[Deps]) -> str:
207207
return ctx.deps.country
208208

209209

210-
def get_weather(city: str) -> str:
211-
return 'sunny'
210+
class WeatherArgs(BaseModel):
211+
city: str
212+
213+
214+
def get_weather(args: WeatherArgs) -> str:
215+
if args.city == 'Mexico City':
216+
return 'sunny'
217+
else:
218+
return 'unknown' # pragma: no cover
212219

213220

214221
@dataclass

0 commit comments

Comments
 (0)