🐛 Bug Report: LiteLLM instrumentation never ends the span when a call is interrupted (KeyboardInterrupt, asyncio cancellation)

オープン 初心者向け
#4,597 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@rupak-eng がすでに取り組んでいます。

2026年10月11日 から。

  • #4600 @rupak-eng による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
80/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

opentelemetry/instrumentation/litellm/__init__.py の _wrap_factory と _awrap_factory から始めます。ここでは、completion と embedding の呼び出しの前後で span が処理されます。提供された KeyboardInterrupt の例で割り込みを再現し、その後、パッケージの LiteLLM 計装テストを追加または更新して、割り込みられた同期呼び出しと非同期呼び出しをカバーします。完了の基準は、元の例外の動作を変更することなく、span が終了され、割り込みが記録されることです。

索引モデルが issue の本文から書いたものです。

説明

Which component is this bug for?

Traceloop SDK

📜 Description

Package: opentelemetry-instrumentation-litellm 0.62.4 (not in the component list, so I picked the closest option).

In the LiteLLM instrumentation, _wrap_factory and _awrap_factory (opentelemetry/instrumentation/litellm/__init__.py, lines 681 and 717 at 0.62.4) end the span only in except Exception (via _handle_exception) or after a successful return:

        try:
            response = wrapped(*args, **kwargs)
        except Exception as error:
            _handle_exception(span, metrics, error, model, kwargs)
            raise
        finally:
            context_api.detach(token)

KeyboardInterrupt (Ctrl-C) and asyncio.CancelledError (a cancelled task, a timeout via asyncio.wait_for, a client disconnect in a web server) derive from BaseException, not Exception, so the span is never ended and never exported. This affects completion, acompletion, embedding and aembedding. Cancelling LLM calls is common in async servers, so these calls silently disappear from the traces; the open spans also stay referenced.

This is the same pattern as #4576 (other instrumentations), here in the LiteLLM package.

👟 Reproduction steps

pip install litellm opentelemetry-instrumentation-litellm opentelemetry-sdk httpx

import os
os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"
import httpx, litellm, openai
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
from opentelemetry.instrumentation.litellm import LiteLLMInstrumentor

class Count(SpanProcessor):
    started = ended = 0
    def on_start(self, span, parent_context=None): Count.started += 1
    def on_end(self, span): Count.ended += 1

provider = TracerProvider()
provider.add_span_processor(Count())
LiteLLMInstrumentor().instrument(tracer_provider=provider)

def respond(request):          # local stand-in: the user presses Ctrl-C while waiting for the server
    raise KeyboardInterrupt
client = openai.OpenAI(api_key="x", max_retries=0, http_client=httpx.Client(transport=httpx.MockTransport(respond)))

for call in (lambda: litellm.completion(model="openai/gpt-4o-mini", messages=[{"role": "user", "content": "hi"}],
                                        client=client, api_key="x"),
             lambda: litellm.embedding(model="openai/text-embedding-3-small", input=["hi"], client=client, api_key="x")):
    Count.started = Count.ended = 0
    try:
        call()
    except KeyboardInterrupt:
        pass
    print("spans started", Count.started, "ended", Count.ended)
👍 Expected behavior

The span ends in every case, with the interruption recorded (e.g. error.type = KeyboardInterrupt / CancelledError, status ERROR), e.g. by catching BaseException in the wrappers (re-raising it unchanged) or ending the span in finally.

👎 Actual Behavior with Screenshots
F  plain call interrupted by KeyboardInterrupt
   app got: KeyboardInterrupt
   spans started 1, ended 0
G  embedding interrupted by KeyboardInterrupt
   app got: KeyboardInterrupt
   spans started 1, ended 0

The OpenInference LiteLLM instrumentation ends the span in the same situation.

🤖 Python Version

3.12

📃 Provide any additional context for the Bug.

opentelemetry-instrumentation-litellm 0.62.4 (the latest release), litellm 1.104.2. The same applies to acompletion / aembedding cancelled with asyncio.CancelledError (same except Exception in _awrap_factory).

👀 Have you spent some time to check if this bug has been raised before?
  • I checked and didn't find similar issue
Are you willing to submit PR?

None

主要言語
Python
スター
7.5k
フォーク
1.2k
平均マージ
2日 6時間
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

traceloop/openllmetry のほかの issue

traceloop/openllmetry の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。