OllamaModelBackend: use TaskGroup for structured cancellation in generate_from_raw

Đang mở
#1,164 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
50/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
ollama, python
Lĩnh vực
backend

Hướng nghiên cứu

Start in OllamaModelBackend.generate_from_raw, where the asyncio.gather call from PR #1163 runs the parallel Ollama requests. The issue proposes replacing it with an asyncio.TaskGroup so that one failure cancels the remaining requests. Decide how callers that catch ConnectionError will handle the ExceptionGroup, then confirm that a failed request cancels the others.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

area/backends enhancement p2

Background

PR #1163 fixed generate_from_raw to propagate exceptions instead of silently swallowing them into empty ModelOutputThunk(value="") (issue #597). The fix uses asyncio.gather(*coroutines) (default return_exceptions=False).

Problem

With asyncio.gather(return_exceptions=False), when one coroutine raises, the other in-flight coroutines are not cancelled — they continue running on the event loop until completion, but their results are discarded. For N parallel Ollama HTTP requests, this means up to N−1 requests complete and their results are thrown away.

This is a pre-existing behaviour of asyncio.gather made visible by the fix in #1163. It was not introduced by that PR; under the old code all N tasks always ran to completion (whether they failed or not).

Desired behaviour

Python 3.12 (the project's minimum version) has asyncio.TaskGroup, which provides structured concurrency: if any task in the group raises, the remaining tasks are cancelled. This avoids wasted compute and network bandwidth.

async with asyncio.TaskGroup() as tg:
    tasks = [tg.create_task(co) for co in coroutines]
responses = [t.result() for t in tasks]

Caveats

  • TaskGroup raises ExceptionGroup (not the raw exception) on failure. Callers that do except ConnectionError would need to use except* or ExceptionGroup handling. This is a semantic change on top of the change in #1163.
  • Consider whether to unwrap single-exception groups to preserve the plain-exception API surface.

Related

  • Closes #597 (via #1163)
  • PR #1163 — introduced gather without return_exceptions
Ngôn ngữ chính
Python
Star
1.8k
Fork
161
Merge trung bình
4 ngày 3 giờ
Pull request đã merge (30 ngày)
45

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của generative-computing/mellea

Tất cả issue của generative-computing/mellea

Issue tương tự

Thêm issue về Python

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.