a2aproject / a2aproject/a2a-samples

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

Đang mở
#38 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Jupyter Notebook
Star
1.8k
Fork
751
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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])
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.