a2aproject / a2aproject/a2a-samples

langgraph Stream example is wrong

Aberta Para iniciantes
#309 0 comentários 0 reações 0 responsáveis Ver no GitHub
Linguagem predominante
Jupyter Notebook
Estrelas
1.8k
Forks
751
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

### What happened?

Hi team,

Bug: stream does not work without async for – should use astream for async iteration

Code:
samples/python/agents/langgraph/app/agent.py

I encountered an issue while using the `stream` function in an async context. Here's the original code I used:

```python
async def stream(self, query, context_id) -> AsyncIterable[dict[str, Any]]:
inputs = {'messages': [('user', query)]}
config = {'configurable': {'thread_id': context_id}}

for item in self.graph.stream(inputs, config, stream_mode='values'):
message = item['messages'][-1]
if (
isinstance(message, AIMessage)
and message.tool_calls
and len(message.tool_calls) > 0
):
...
```

However, this **does not stream the response** as expected. It only works properly when I change the line to:

```python
async for item in self.graph.astream(inputs, config, stream_mode='values'):
```

It looks like `graph.stream` returns a synchronous iterator, while `graph.astream` is the correct async generator required for streaming in async functions.

For a complete test and reproduction case, please refer to my repo:
👉 [https://github.com/johnson7788/LangGraphA2A](https://github.com/johnson7788/LangGraphA2A)

Hope this helps improve the documentation or implementation!

Thanks!

### Relevant log output

```shell

```

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

Guia de contribuição

Abrir o guia de contribuição

Direção de pesquisa

The issue is in the file `samples/python/agents/langgraph/app/agent.py`. Look for the `stream` method and the line using `self.graph.stream`. Change it to use `self.graph.astream` for async iteration. Verify the fix by checking the linked repository for a reproduction case and ensure the async streaming works as expected.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
ai-infra-agents, documentation
Tipo de issue
Bug
Dificuldade
1/5
Tempo estimado
Menos de uma hora
Status de atividade
Estagnada
Clareza
Claramente especificada
Facilidade para iniciantes
75/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.