[AI Projects] AIProjectInstrumentor: non-streamed responses.create raises AttributeError ('NonRecordingSpan' object has no attribute 'attributes') when the span is not sampled

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

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

まだ誰も着手していません。

評価

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

調査の方向性

azure/ai/projects/telemetry/_responses_instrumentor.py の _append_to_message_attribute から始め、span の処理を _set_span_attribute_safe およびストリーム用ラッパーと比較してください。OpenTelemetry の ALWAYS_OFF sampler と、ストリーミングを使わない responses.create 呼び出しで再現してください。正常なレスポンスの後に記録されない span が例外を発生させなくなり、レスポンスも引き続き機能すれば完了です。

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

説明

AI Projects Client customer-reported question

Package: azure-ai-projects 2.10.0. Also seen on 2.9.0, and the helper is unchanged on main as of 2026-11-05.
Also installed: openai 3.35.1 to 3.46.0, opentelemetry-sdk 1.40.0, azure-core-tracing-opentelemetry 1.0.0b13, Python 3.12.

What happens

With AIProjectInstrumentor enabled, a non-streamed responses.create raises whenever the current span is not recording, for example when a sampler drops the trace:

AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'

It raises after the service has answered with HTTP 200, so the caller loses a completed and billed response. Streamed calls are not affected.

Cause

In azure/ai/projects/telemetry/_responses_instrumentor.py (line 681 in 2.10.0), _append_to_message_attribute reads span.span_instance.attributes without checking span.span_instance.is_recording(). OpenTelemetry's NonRecordingSpan has no attributes. The neighbouring helpers, such as _set_span_attribute_safe and the stream wrappers, already guard with is_recording().

_responses_instrumentor.py:4777 inner -> self.trace_responses_create(...)
_responses_instrumentor.py:2248 trace_responses_create -> self._add_message_event(...)
_responses_instrumentor.py:765  _add_message_event -> self._append_to_message_attribute(...)
_responses_instrumentor.py:681  _append_to_message_attribute
    existing_value = span.span_instance.attributes.get(attribute_name) if span.span_instance.attributes else None
AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'

Repro (no network)

pip install azure-ai-projects==2.10.0 opentelemetry-sdk==1.40.0 azure-core-tracing-opentelemetry==1.0.0b13

import os

import httpx2
import openai
from azure.ai.projects.telemetry import AIProjectInstrumentor
from azure.core.settings import settings
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF

# Every span non-recording, as when a sampler drops the trace.
trace.set_tracer_provider(TracerProvider(sampler=ALWAYS_OFF))
settings.tracing_implementation = "opentelemetry"
os.environ["AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING"] = "true"
AIProjectInstrumentor().instrument()

RESPONSE = {
    "id": "resp_1", "object": "response", "created_at": 0, "status": "completed", "model": "m",
    "output": [{"type": "message", "id": "msg_1", "status": "completed", "role": "assistant",
                "content": [{"type": "output_text", "text": "done", "annotations": []}]}],
    "parallel_tool_calls": True, "tool_choice": "auto", "tools": [],
}
calls = []
def handler(request):
    calls.append(request.url)
    return httpx2.Response(200, json=RESPONSE)

client = openai.OpenAI(api_key="test", base_url="https://example.invalid/v1",
                       http_client=httpx2.Client(transport=httpx2.MockTransport(handler)))
try:
    client.responses.create(input="hi")
    print("no error")
except AttributeError as exc:
    print(f"HTTP calls: {len(calls)}; then: AttributeError: {exc}")

Output:

HTTP calls: 1; then: AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'

Why it matters in practice

When OTEL_TRACES_SAMPLER is unset, azure-monitor-opentelemetry 1.8.8 defaults to a rate-limited sampler at 5 traces/s. In an ordinary App Insights setup, some of these spans are therefore not recording, and production calls fail intermittently after the model has already run.

Expected

Telemetry should never change the outcome of the call. When the span is not recording, skip the attribute aggregation, as the other helpers already do.

Workaround

Leave AIProjectInstrumentor off (AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING unset), or stream the call.

主要言語
Python
スター
5.6k
フォーク
3.4k
平均マージ
2日 23時間
マージ済み PR(30日)
184

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

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

Azure/azure-sdk-for-python のほかの issue

Azure/azure-sdk-for-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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