Backtestsopenai-agents-pythonrun-20261024-184640#4635
Allow SingleAgentVoiceWorkflow to receive Runner context
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches make the identical core change: add a keyword-only `context` parameter to `SingleAgentVoiceWorkflow.__init__`, store it, and forward it to `Runner.run_streamed` on every turn. The independent tests confirm both fully resolve the issue (all eight pass on A and B), so correctness is a wash. The differences are in tests and craft. Patch B's test directly exercises every scenario the issue names: dynamic instructions, a context-dependent tool, a handoff `is_enabled` predicate, a second turn after the current agent changes via handoff, identity of the context object across turns, and a separate test enforcing keyword-only. Patch A's test covers only a tool over two turns and, more worryingly, imports `tool` from `agents.decorators`, which is not the visible public decorator (`function_tool`, exported from `agents`, is what the rest of the codebase and Patch B use); if that module does not exist, the import would break collection of the whole existing test file. A's test also indexes `item["output"]` on a `TResponseInputItem` union, which is fragile under the repo's type checking. On craft, B keeps the existing `Agent[Any]` convention and types `context: Any | None` with a clear docstring stating it is forwarded as-is to `Runner.run_streamed`, matching the issue's 'normal Runner behavior' requirement. A swaps to `Agent[TContext]`/`TContext | None`, which reads as tighter typing but is a function-scoped TypeVar on a non-generic class whose values are stored on instance attributes, a slightly awkward leak rather than a real gain. Net: equivalent fix, but B has meaningfully stronger and safer tests and cleaner consistency with surrounding code.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
3m 49s
Wall-clock time in the container
Full test suite
No new failures
3 passed · 0 skipped · 3 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
94vs 53 merged
Changed lines, ours vs merged
The issue
View on GitHubPlease read this first
- Have you read the docs? Yes.
- Have you searched for related issues and PRs? Yes.
Related: #819 documented this limitation and was later closed after the reporter worked around it by creating a custom VoiceWorkflowBase implementation. I could not find an existing PR that adds context support to SingleAgentVoiceWorkflow itself.
Describe the feature
SingleAgentVoiceWorkflow is the convenience path for a single-agent voice workflow, but its internal call is currently fixed to:
Runner.run_streamed(self._current_agent, self._input_history)
That means an agent that relies on normal Runner context cannot use the convenience workflow directly. Context-dependent instructions, tools, handoffs, and is_enabled predicates require users to copy the workflow implementation into a custom subclass just to pass context=....
A narrow API addition would be to accept an optional keyword-only context when constructing SingleAgentVoiceWorkflow and forward that same context to each internal Runner.run_streamed() call.
For example:
context = UserContext(user_id="user-123")
workflow = SingleAgentVoiceWorkflow(agent, context=context)
pipeline = VoicePipeline(workflow=workflow)
Desired semantics
- Existing
SingleAgentVoiceWorkflow(agent)andSingleAgentVoiceWorkflow(agent, callbacks)calls remain unchanged. context=is optional and keyword-only.- The supplied context is forwarded to every streamed Runner turn, including later turns after the workflow updates its input history/current agent.
- Context behavior remains the normal Runner behavior; the voice layer does not introduce a separate context wrapper or lifecycle.
- Custom
VoiceWorkflowBaseimplementations remain the supported path for broader per-turn Runner customization.
Non-goals
- no change to
VoicePipeline.run()arguments; - no new voice-specific context abstraction;
- no automatic copying or serialization of application context;
- no broader
RunConfig, session, or conversation-state API in this change.
This keeps the convenience workflow useful for context-dependent agents without expanding the voice pipeline contract.
The two patches
src/agents/voice/workflow.pyindex b3b9734e..87750062 100644@@ -66,16 +66,26 @@ class SingleAgentVoiceWorkflow(VoiceWorkflowBase): custom configs), subclass `VoiceWorkflowBase` and implement your own logic. """ - def __init__(self, agent: Agent[Any], callbacks: SingleAgentWorkflowCallbacks | None = None):+ def __init__(+ self,+ agent: Agent[Any],+ callbacks: SingleAgentWorkflowCallbacks | None = None,+ *,+ context: Any | None = None,+ ): """Create a new single agent voice workflow. Args: agent: The agent to run. callbacks: Optional callbacks to call during the workflow.+ context: Optional context object passed to every `Runner.run_streamed()` call made by+ this workflow. It is forwarded as-is, exactly like the `context` argument to+ `Runner.run_streamed()`. """ self._input_history: list[TResponseInputItem] = [] self._current_agent = agent self._callbacks = callbacks+ self._context = context async def run(self, transcription: str) -> AsyncIterator[str]: if self._callbacks is not None:@@ -90,7 +100,9 @@ class SingleAgentVoiceWorkflow(VoiceWorkflowBase): ) # Run the agent- result = Runner.run_streamed(self._current_agent, self._input_history)+ result = Runner.run_streamed(+ self._current_agent, self._input_history, context=self._context+ ) # Stream the text from the result async for chunk in VoiceWorkflowHelper.stream_text_from(result):tests/voice/test_workflow.pyindex af270079..f764010d 100644@@ -5,10 +5,15 @@ import json import pytest from inline_snapshot import snapshot -from agents import Agent+from agents import Agent, RunContextWrapper, function_tool, handoff from agents.testing import ScriptedModel -from ..test_responses import get_function_tool, get_function_tool_call, get_text_message+from ..test_responses import (+ get_function_tool,+ get_function_tool_call,+ get_handoff_tool_call,+ get_text_message,+) try: from agents.voice import SingleAgentVoiceWorkflow@@ -136,3 +141,72 @@ async def test_single_agent_workflow(monkeypatch) -> None: ] ) assert workflow._current_agent == agent+++class UserContext:+ def __init__(self, user_id: str) -> None:+ self.user_id = user_id+++@pytest.mark.asyncio+async def test_single_agent_workflow_forwards_context_to_every_turn() -> None:+ seen: list[tuple[str, object]] = []++ def dynamic_instructions(ctx: RunContextWrapper[UserContext], agent: Agent[UserContext]) -> str:+ seen.append((f"instructions:{agent.name}", ctx.context))+ return f"Help user {ctx.context.user_id}"++ @function_tool+ def lookup_user(ctx: RunContextWrapper[UserContext]) -> str:+ seen.append(("tool", ctx.context))+ return ctx.context.user_id++ def handoff_enabled(ctx: RunContextWrapper[UserContext], agent: Agent[UserContext]) -> bool:+ seen.append(("handoff_enabled", ctx.context))+ return ctx.context.user_id == "user-123"++ model = ScriptedModel()+ second_agent = Agent[UserContext](+ "second_agent", model=model, instructions=dynamic_instructions+ )+ first_agent = Agent[UserContext](+ "first_agent",+ model=model,+ instructions=dynamic_instructions,+ tools=[lookup_user],+ handoffs=[handoff(second_agent, is_enabled=handoff_enabled)],+ )+ model.extend(+ [+ [get_function_tool_call("lookup_user", "{}")],+ [get_text_message("hello"), get_handoff_tool_call(second_agent)],+ [get_text_message("from second")],+ ]+ )++ context = UserContext("user-123")+ workflow = SingleAgentVoiceWorkflow(first_agent, context=context)++ output = [chunk async for chunk in workflow.run("transcription_1")]+ assert output == ["hello", "from second"]+ assert workflow._current_agent is second_agent+ assert ("tool", context) in seen+ assert ("handoff_enabled", context) in seen+ assert ("instructions:first_agent", context) in seen+ assert ("instructions:second_agent", context) in seen++ # A later transcription starts a new streamed run from the updated current agent and must+ # receive the same context object.+ seen.clear()+ model.enqueue([get_text_message("again")])+ output = [chunk async for chunk in workflow.run("transcription_2")]+ assert output == ["again"]+ assert seen == [("instructions:second_agent", context)]+ assert all(value is context for _, value in seen)+++@pytest.mark.asyncio+async def test_single_agent_workflow_context_is_keyword_only() -> None:+ agent = Agent("agent", model=ScriptedModel())+ with pytest.raises(TypeError):+ SingleAgentVoiceWorkflow(agent, None, object()) # type: ignore[misc]
src/agents/voice/workflow.pyindex b3b9734e9..68b9cb82e 100644@@ -2,12 +2,12 @@ from __future__ import annotations import abc from collections.abc import AsyncIterator-from typing import Any from ..agent import Agent from ..items import TResponseInputItem from ..result import RunResultStreaming from ..run import Runner+from ..run_context import TContext class VoiceWorkflowBase(abc.ABC):@@ -66,16 +66,24 @@ class SingleAgentVoiceWorkflow(VoiceWorkflowBase): custom configs), subclass `VoiceWorkflowBase` and implement your own logic. """ - def __init__(self, agent: Agent[Any], callbacks: SingleAgentWorkflowCallbacks | None = None):+ def __init__(+ self,+ agent: Agent[TContext],+ callbacks: SingleAgentWorkflowCallbacks | None = None,+ *,+ context: TContext | None = None,+ ): """Create a new single agent voice workflow. Args: agent: The agent to run. callbacks: Optional callbacks to call during the workflow.+ context: Optional application context forwarded to every agent run. """ self._input_history: list[TResponseInputItem] = [] self._current_agent = agent self._callbacks = callbacks+ self._context = context async def run(self, transcription: str) -> AsyncIterator[str]: if self._callbacks is not None:@@ -90,7 +98,11 @@ class SingleAgentVoiceWorkflow(VoiceWorkflowBase): ) # Run the agent- result = Runner.run_streamed(self._current_agent, self._input_history)+ result = Runner.run_streamed(+ self._current_agent,+ self._input_history,+ context=self._context,+ ) # Stream the text from the result async for chunk in VoiceWorkflowHelper.stream_text_from(result):tests/voice/test_workflow.pyindex af2700796..2f561a2a1 100644@@ -5,7 +5,8 @@ import json import pytest from inline_snapshot import snapshot -from agents import Agent+from agents import Agent, RunContextWrapper+from agents.decorators import tool from agents.testing import ScriptedModel from ..test_responses import get_function_tool, get_function_tool_call, get_text_message@@ -136,3 +137,35 @@ async def test_single_agent_workflow(monkeypatch) -> None: ] ) assert workflow._current_agent == agent+++@pytest.mark.asyncio+async def test_single_agent_workflow_forwards_context_on_every_turn() -> None:+ @tool+ def read_user_id(ctx: RunContextWrapper[dict[str, str]]) -> str:+ """Return the current user ID."""+ return ctx.context["user_id"]++ model = ScriptedModel()+ model.extend(+ [+ [get_function_tool_call("read_user_id", "{}", call_id="context_call_1")],+ [get_text_message("first turn done")],+ [get_function_tool_call("read_user_id", "{}", call_id="context_call_2")],+ [get_text_message("second turn done")],+ ]+ )+ agent = Agent("context_agent", model=model, tools=[read_user_id])+ workflow = SingleAgentVoiceWorkflow(agent, context={"user_id": "user-123"})++ first_output = [chunk async for chunk in workflow.run("first transcription")]+ second_output = [chunk async for chunk in workflow.run("second transcription")]++ assert first_output == ["first turn done"]+ assert second_output == ["second turn done"]+ tool_outputs = [+ item["output"]+ for item in workflow._input_history+ if item.get("type") == "function_call_output"+ ]+ assert tool_outputs == ["user-123", "user-123"]