Skip to content

Python: [Bug]: Hosted agent sample reuses a single workflow instance and breaks under parallel requests #4766

@ManniArora

Description

@ManniArora

Description

The Python sample appears to create or reuse a workflow instance in a way that is not safe for concurrent execution:

python/samples/05-end-to-end/hosted_agents/writer_reviewer_agents_in_workflow/main.py

When the hosted agent receives parallel requests, it can attempt to execute the same workflow instance concurrently and fails with:

RuntimeError: Workflow is already running. Concurrent executions are not allowed.

This makes the sample effectively broken for hosted scenarios where parallel requests are expected.

Link
https://github.com/microsoft/agent-framework/blob/main/python/samples/05-end-to-end/hosted_agents/writer_reviewer_agents_in_workflow/main.py#L55

The sample should be safe for parallel requests in a hosted agent scenario.

Instead of reusing a single workflow instance across requests, it should create a fresh workflow per execution, for example:

async def main() -> None:
    client = AzureOpenAIChatClient(credential=AzureCliCredential())
    await from_agent_framework(lambda: create_workflow(client)).run_async()

Code Sample

Error Messages / Stack Traces

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/azure/ai/agentserver/agentframework/_agent_framework.py", line 249, in stream_updates
    async for event in streaming_converter.convert(updates):
  File "/usr/local/lib/python3.12/site-packages/azure/ai/agentserver/agentframework/models/agent_framework_output_streaming_converter.py", line 379, in convert
    async for group in chunk_on_change(updates, is_changed):
  File "/usr/local/lib/python3.12/site-packages/azure/ai/agentserver/agentframework/models/utils/async_iter.py", line 39, in chunk_on_change
    async for group in chunk_by_key(source, lambda x: x, key_equal=key_equal):
  File "/usr/local/lib/python3.12/site-packages/azure/ai/agentserver/agentframework/models/utils/async_iter.py", line 69, in chunk_by_key
    pending = await it.__anext__()
              ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/agent_framework/_workflows/_agent.py", line 203, in run_stream
    async for update in self._run_stream_impl(
  File "/usr/local/lib/python3.12/site-packages/agent_framework/_workflows/_agent.py", line 282, in _run_stream_impl
    async for event in event_stream:
  File "/usr/local/lib/python3.12/site-packages/agent_framework/_workflows/_workflow.py", line 529, in run_stream
    self._ensure_not_running()
  File "/usr/local/lib/python3.12/site-packages/agent_framework/_workflows/_workflow.py", line 234, in _ensure_not_running
    raise RuntimeError("Workflow is already running. Concurrent executions are not allowed.")
RuntimeError: Workflow is already running. Concurrent executions are not allowed.

Package Versions

azure-ai-agentserver-agentframework==1.0.0b14

Python Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions