Skip to content

Commit 4266443

Browse files
committed
fix(workflows): drop assistant function calls orphaned by role filtering
Role-only filtering kept assistant(function_call) while dropping its tool(function_result), producing an invalid call-without-result transcript that providers reject on replay when the response is persisted through a session. Assistant messages whose contents are entirely function-call envelopes are now dropped alongside the non-assistant messages, so the public response carries only user-facing assistant output with no dangling calls. Regression test covers call → tool result → final. Signed-off-by: Manohar Paturi <[email protected]>
1 parent cf2a1c0 commit 4266443

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

‎python/packages/core/agent_framework/_workflows/_agent.py‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@
5252
logger = logging.getLogger(__name__)
5353

5454

55+
def _is_orphaned_function_call(message: "Message") -> bool:
56+
"""True when an assistant message is only a function call whose tool result
57+
is not assistant-role, so keeping the call alone would produce an invalid
58+
(unpaired) transcript for providers that validate call/result history."""
59+
if message.role != "assistant":
60+
return False
61+
contents = list(getattr(message, "contents", []) or [])
62+
if not contents:
63+
return False
64+
# Orphaned only when every content is a function-call envelope
65+
return all(
66+
getattr(content, "type", None) in ("function_call", "function_approval_response")
67+
for content in contents
68+
)
69+
70+
5571
class WorkflowAgent(BaseAgent):
5672
"""An `Agent` subclass that wraps a workflow and exposes it as an agent."""
5773

@@ -591,7 +607,14 @@ def _convert_workflow_events_to_agent_response(
591607
# are intentionally excluded. System prompts and tool results are
592608
# internal workflow artifacts; user messages would be re-emitted
593609
# (e.g., from GroupChat orchestrators that include full conversation history).
594-
assistant_messages = [msg for msg in data.messages if msg.role == "assistant"]
610+
# Assistant messages that are bare function calls are also dropped
611+
# when their tool result is not assistant-role: a call without its
612+
# result is an invalid transcript for providers that validate
613+
# call/result pairing on replay.
614+
assistant_messages = [
615+
msg for msg in data.messages
616+
if msg.role == "assistant" and not _is_orphaned_function_call(msg)
617+
]
595618
if assistant_messages:
596619
messages.extend(assistant_messages)
597620
raw_representations.append(data.raw_representation)
@@ -609,7 +632,12 @@ def _convert_workflow_events_to_agent_response(
609632
raw_representations.append(data.raw_representation)
610633
elif is_instance_of(data, list[Message]):
611634
chat_messages = cast(list[Message], data)
612-
assistant_messages = [msg for msg in chat_messages if msg.role == "assistant"]
635+
# Keep tool results that pair with surviving assistant calls so the
636+
# transcript stays valid; drop user/system and orphaned calls.
637+
assistant_messages = [
638+
msg for msg in chat_messages
639+
if msg.role == "assistant" and not _is_orphaned_function_call(msg)
640+
]
613641
if assistant_messages:
614642
messages.extend(assistant_messages)
615643
# raw_representation of a filtered list must not leak the

‎python/packages/core/tests/workflow/test_workflow_agent.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,46 @@ async def mixed_list_executor(
26822682
for rep in result.raw_representation or []:
26832683
assert rep is None or (isinstance(rep, Message) and rep.role == "assistant")
26842684

2685+
async def test_workflow_as_agent_drops_orphaned_function_calls(self) -> None:
2686+
"""assistant(function_call) whose tool result is tool-role must not survive filtering.
2687+
2688+
Keeping the call without its result would produce an invalid transcript for
2689+
providers that validate call/result pairing on replay (eavanvalkenburg's review).
2690+
"""
2691+
2692+
@executor
2693+
async def tool_transcript_executor(
2694+
messages: list[Message],
2695+
ctx: WorkflowContext[Never, list[Message]], # type: ignore[valid-type]
2696+
) -> None:
2697+
await ctx.yield_output([
2698+
Message(role="assistant", contents=[
2699+
Content.from_function_call(call_id="call-1", name="get_weather", arguments={"city": "Paris"}),
2700+
]),
2701+
Message(role="tool", contents=[
2702+
Content.from_function_result(call_id="call-1", result="18C"),
2703+
]),
2704+
Message(role="assistant", contents=[Content.from_text("It is 18C in Paris.")]),
2705+
])
2706+
2707+
workflow = WorkflowBuilder(start_executor=tool_transcript_executor).build()
2708+
agent = workflow.as_agent("tool-transcript-agent")
2709+
2710+
result = await agent.run("weather")
2711+
2712+
# The orphaned call is dropped; the final user-facing answer survives.
2713+
assert all(msg.role == "assistant" for msg in result.messages)
2714+
assert not any(
2715+
getattr(content, "type", None) == "function_call"
2716+
for msg in result.messages
2717+
for content in msg.contents
2718+
)
2719+
assert any(
2720+
"18C" in (getattr(content, "text", "") or "")
2721+
for msg in result.messages
2722+
for content in msg.contents
2723+
)
2724+
26852725
async def test_workflow_as_agent_filters_single_non_assistant_message(self) -> None:
26862726
"""Verify WorkflowAgent filters a single Message when role is not assistant."""
26872727

0 commit comments

Comments
 (0)