Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ to include examples, links to docs, or any other relevant information.

### Fixed

- `temporalio.contrib.deepagents` now runs separate tool calls independently
when they use the same tool and arguments.
- `StrandsPlugin` now disables Botocore retries for its default Bedrock model so
model request retries are handled exclusively by Temporal.
- `temporalio.contrib.openai_agents` now honors the `retry-after-ms` and
Expand Down
6 changes: 5 additions & 1 deletion temporalio/contrib/deepagents/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ async def call_tool(
**opts: Any,
) -> _activity.ToolActivityOutput:
"""Dispatch one tool call, reusing a cached result across continue-as-new."""
key = _serde.cache_key("tool", activity_input.tool_name, activity_input.args)
key = _serde.cache_key(
"tool",
activity_input.tool_call_id,
[activity_input.tool_name, activity_input.args],
)
hit, cached = _serde.cache_lookup(key)
if hit:
return _activity.ToolActivityOutput(message=cached)
Expand Down
65 changes: 65 additions & 0 deletions tests/contrib/deepagents/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
from temporalio.contrib.deepagents import ( # noqa: E402
DeepAgentsPlugin,
activity_as_tool,
run_deep_agent,
tool_as_activity,
)
from temporalio.contrib.deepagents._tools import warn_unwrapped_tools # noqa: E402
from temporalio.contrib.deepagents.testing import mock_model_provider # noqa: E402

INVOKE_TOOL = "deepagents.invoke_tool"

Expand Down Expand Up @@ -159,6 +161,22 @@ async def run(self, city: str) -> str:
return str(result["messages"][-1].content)


@workflow.defn
class RepeatedToolCallWorkflow:
@workflow.run
async def run(self) -> str:
tool = tool_as_activity(
pairing_weather, start_to_close_timeout=timedelta(seconds=10)
)
agent = create_deep_agent(model="fake:model", tools=[tool])
result = await run_deep_agent(
agent,
{"messages": [{"role": "user", "content": "Check Paris twice."}]},
continue_as_new_after=10_000,
)
return str(result["messages"][-1].content)


@pytest.mark.asyncio
async def test_wrapped_tool_result_pairs_with_model_tool_call_id(
env: WorkflowEnvironment,
Expand Down Expand Up @@ -236,3 +254,50 @@ def provider(_model_name: str) -> RecordingModel:
0
].tool_call_id
assert "weather:Paris" in str(tool_messages[0].content)


@pytest.mark.asyncio
async def test_repeated_tool_calls_with_same_args_both_run(
env: WorkflowEnvironment,
) -> None:
from langchain_core.messages import AIMessage

responses = [
AIMessage(
content="",
tool_calls=[
{
"name": "pairing_weather",
"args": {"city": "Paris"},
"id": "first-call",
}
],
),
AIMessage(
content="",
tool_calls=[
{
"name": "pairing_weather",
"args": {"city": "Paris"},
"id": "second-call",
}
],
),
AIMessage(content="done"),
]
plugin = DeepAgentsPlugin(model_provider=mock_model_provider(responses))
async with Worker(
env.client,
task_queue="da-repeated-tool-calls",
workflows=[RepeatedToolCallWorkflow],
plugins=[plugin],
):
handle = await env.client.start_workflow(
RepeatedToolCallWorkflow.run,
id=f"da-repeated-tool-calls-{uuid.uuid4()}",
task_queue="da-repeated-tool-calls",
)
assert await handle.result() == "done"

counts = await count_scheduled_activities(handle)
assert counts[INVOKE_TOOL] == 2, counts