Skip to content

Python: [Bug]: _convert_workflow_event_to_agent_response_updates emits user input again #4261

@leelakarthik

Description

@leelakarthik

Description

When agent responds to user query, it appends the user input with assistant response when using streaming response along with workflow.as_agent()

Sample Non Streaming response

-> Ask a question (or 'exit' to quit): hi
[Response]:
hiHello! Welcome to our school. I'm the Principal here. How can I help you today? If you have questions about mathematics, physics, or social science, I have excellent teachers who c
an assist you. 
                                                                                                                                       
-> Ask a question (or 'exit' to quit): a=3, b=5, what is a-b

[Response]:
hiHello! Welcome to our school. I'm the Principal here. How can I help you today? If you have questions about mathematics, physics, or social science, I have excellent teachers who c
an assist you.a=3, b=5, what is a-bI need to find a - b when a = 3 and b = 5.                                                                                                         
**Solution:**
a - b = 3 - 5 = **-2**The Maths Teacher has answered your question. When a = 3 and b = 5, a - b = -2. Is there anything else you'd like to learn about?

-> Ask a question (or 'exit' to quit): exit
Exiting.

Sample Streaming response

-> Ask a question (or 'exit' to quit): hi

[Principal]:

hi
Hello! Welcome to our school. I'm the Principal here. How can I help you today? If you have any questions about mathematics, physics, or social science, I'd be happy to connect you w
ith the appropriate teacher.                                                                                                                                                          
-> Ask a question (or 'exit' to quit): a=3, b=5, what is a-b+a

[Principal]:

hi
Hello! Welcome to our school. I'm the Principal here. How can I help you today? If you have any questions about mathematics, physics, or social science, I'd be happy to connect you w
ith the appropriate teacher.                                                                                                                                                          a=3, b=5, what is a-b+a

[Maths Teacher]:

I need to find the value of a - b + a, where a = 3 and b = 5.

Let me substitute the values:

a - b + a = 3 - 5 + 3

Now I'll calculate step by step:
- 3 - 5 = -2
- -2 + 3 = **1**

The answer is **1**.

[Principal]:

The Maths Teacher has answered your question. The value of a - b + a, where a = 3 and b = 5, is 1. Let me know if you have any other questions!

-> Ask a question (or 'exit' to quit): exit
Exiting.

Code Sample

from agent_framework import Agent
from agent_framework_anthropic import AnthropicClient
from anthropic import AsyncAnthropicFoundry
from agent_framework_orchestrations import GroupChatBuilder

client = AnthropicClient(
    anthropic_client=AsyncAnthropicFoundry(timeout=120),
    model_id="claude-opus-4-5",
)

maths_agent = Agent(
    client=client,
    instructions="You are a Maths teacher. Answer the user's question about maths. If you don't know the answer, say you don't know.",
    name="Maths Teacher",
    description=("Maths expert. Answers questions about mathematics and provides explanations."),
    default_options={"max_tokens": 4096},
)

physics_agent = Agent(
    client=client,
    instructions="You are a Physics teacher. Answer the user's question about physics. If you don't know the answer, say you don't know.",
    name="Physics Teacher",
    description=("Physics expert. Answers questions about physics and provides explanations."),
    default_options={"max_tokens": 4096},
)

social_science_agent = Agent(
    client=client,
    instructions="You are a Social Science teacher. Answer the user's question about social science. If you don't know the answer, say you don't know.",
    name="Social Science Teacher",
    description=(
        "Social Science expert. Answers questions about social science and provides explanations."
    ),
    default_options={"max_tokens": 4096},
)

principal_agent = Agent(
    client=client,
    instructions=(
        "You are a Principal of a school. Your job is to answer the user's question by delegating to your teachers: the Maths Teacher, Physics Teacher and Social Science Teacher. "
        "If the question is about mathematics, delegate to the Maths Teacher. If the question is about physics, delegate to the Physics Teacher. If the question is about social science, delegate to the Social Science Teacher. "
        "If you don't know which teacher to delegate to, say you don't know."
    ),
    name="Principal",
    description=(
        "Principal agent that delegates questions to subject-specific teacher agents based on the topic of the question."
    ),
    default_options={"max_tokens": 4096},
)

workflow = GroupChatBuilder(
    orchestrator_agent=principal_agent,
    participants=[maths_agent, physics_agent, social_science_agent],
    max_rounds=10,
).build()

workflow_agent = workflow.as_agent(name="School Principal Agent")


async def chat_nonstream():
    while True:
        message = input("\nAsk a question (or 'exit' to quit): ")
        if message in ["exit", "quit"]:
            print("Exiting.")
            break
        else:
            response = await workflow_agent.run(message)
            print(f"\n[Response]:\n{response.text}")


async def chat_stream():

    while True:
        message = input("\nAsk a question (or 'exit' to quit): ")
        current_agent: str | None = None
        if message in ["exit", "quit"]:
            print("Exiting.")
            break
        else:
            async for chunk in workflow_agent.run(message, stream=True):
                # print(chunk.__dict__)
                author_name = getattr(chunk, "author_name", None)

                if author_name and author_name != current_agent:
                    current_agent = author_name
                    print(f"\n[{current_agent}]:\n")
                if chunk.text:
                    print(chunk.text, end="\n", flush=True)


if __name__ == "__main__":
    import asyncio

    # Streaming version with agent transition markers
    asyncio.run(chat_stream())
    # Non-streaming version
    # asyncio.run(chat_nonstream())

Error Messages / Stack Traces

No error messages

Package Versions

agent-framework==1.0.0rc1

Python Version

python=3.11

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