Skip to content

Fix ai_q_and_a_run graph flow breaking early #3195

@huynguyengl99

Description

@huynguyengl99

Initial Checks

Description

Hi team,

In the example code of ai_q_and_a_run.py from the Human-in-the-loop section, it seems that the following condition:

elif isinstance(node, Answer):  
    print(node.question)
    #> What is the capital of France?
    break

causes the flow to break prematurely.
Since it breaks at the first Answer (which is actually a question prompt), the program only asks the first question and then stops — which might not be the intended behavior.

It looks like removing the break (and possibly the surrounding lines) would allow the flow to continue as expected.

If I misunderstood how this is supposed to work, please let me know.
Thanks again for this great package!

Example Code

async with question_graph.iter(node, state=state, persistence=persistence) as run:
        while True:
            node = await run.next()
            if isinstance(node, End):
                print('END:', node.data)
                history = await persistence.load_all()
                print([e.node for e in history])
                break
            elif isinstance(node, Answer):
                print(node.question)
                #> What is the capital of France?

Python, Pydantic AI & LLM client version

python 3.11
pydantic 2.12.2
llm openai:gpt-4o

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions