Skip to content

How to use RunContextWrapper with VoiceWorkflow #819

Closed

Description

@T-Zaid

Please read this first

  • Have you read the docs?Agents SDK docs
    Yes, I have read the docs and also gone through the examples in repo.
  • Have you searched for related issues? Others may have had similar requests
    Yes, I have but couldn't find anything informational.

Question

Describe your question. Provide details if available.
I am building an agentic system for a store, I have already created the tools, context and all the agents that are required for the use case.

For text based agentic conversation, sending the context is straightforward, that is to pass the context as an argument in Runner.run function. It’s working correctly as well:

context = UserContext(user_id="zaid", email="[email protected]")
result = await Runner.run(current_agent, input_items, context=context)

The place where I’m stuck at is, how to send the context when doing voice-based conversions. I can’t send the context object to voicepipeline.run method, I have thoroughly read the documentation and issue sections in github repository, but I cant find a possible way to use this.

result = await voice_pipeline.run(audio_input=audio_input) // no parameter to send context

Let me know if you require any other additional details.

Activity

  1. github-actions commented on Jun 15, 2025

    @github-actions
    Contributor

    This issue is stale because it has been open for 7 days with no activity.

  2. serranoarevalo commented on Jun 18, 2025

    @serranoarevalo

    @T-Zaid Did you end up fixing this? How did you do it?

  3. T-Zaid commented on Jun 19, 2025

    @T-Zaid
    Author

    Yes, I was able to fulfil the limitation. After digging into the implementation of SingleAgentVoiceWorkflow, I found that the key is to build a custom voice workflow that integrates your own logic, especially to support context.

    Below is my enhanced implementation, which incorporates context into the workflow:

    class SingleAgentVoiceWorkflowWithContext(VoiceWorkflowBase):
        """A simple voice workflow that runs a single agent. Each transcription and result is added to
        the input history with context support.
        """
    
        def __init__(self, agent: Agent[Any], context: RunContextWrapper[Any], callbacks: SingleAgentWorkflowCallbacks | None = None):
            """Create a new single agent voice workflow with context support.
    
            Args:
                agent: The agent to run.
                context: The context to use for the agent.
                callbacks: Optional callbacks to call during the workflow.
            """
            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:
                self._callbacks.on_run(self, transcription)
    
            # Add the transcription to the input history
            self._input_history.append(
                {
                    "role": "user",
                    "content": transcription,
                }
            )
    
            # Run the agent
            result = Runner.run_streamed(self._current_agent, self._input_history, self._context)
    
            # Stream the text from the result
            async for chunk in VoiceWorkflowHelper.stream_text_from(result):
                yield chunk
    
            # Update the input history and current agent
            self._input_history = result.to_input_list()
            self._current_agent = result.last_agent
    

    Now, you can use the workflow in your implementation like so:

        context = UserContext(user_id="zaid", email="[email protected]")
        workflow = SingleAgentVoiceWorkflowWithContext(ShoeStoreAgent, context)
        voice_pipeline = VoicePipeline(workflow=workflow, config=voice_pipeline_config)
    
        result = await voice_pipeline.run(audio_input=audio_input)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions