a2aproject / a2aproject/a2a-samples

`events` is a generator and cannot be indexed with `[-1]`

Abierto
#38 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Jupyter Notebook
Estrellas
1.8k
Forks
751
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

This issue occurs in the **Agent example using ADK**, specifically within the `invoke` function.

Here is the problematic snippet:

```python
events = self._runner.run(
user_id=self._user_id, session_id=session.id, new_message=content
)
if not events or not events[-1].content or not events[-1].content.parts:
return ""
return "\n".join([p.text for p in events[-1].content.parts if p.text])
```

The variable `events` is a **generator**, but the code attempts to access it using `events[-1]`. This is not allowed, as generators do not support indexing or slicing.

Attempting this will raise a `TypeError`:

```
TypeError: 'generator' object is not subscriptable
```

### Workaround

Convert the generator to a list before performing any indexing:

```python
events = list(self._runner.run(
user_id=self._user_id, session_id=session.id, new_message=content
))
if not events or not events[-1].content or not events[-1].content.parts:
return ""
return "\n".join([p.text for p in events[-1].content.parts if p.text])
```

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.