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

Đang mở Phù hợp với người mới
#49,854 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

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

@14H034160212 đang làm issue này rồi.

Từ ngày 7/11/2026.

  • #49879 của @14H034160212 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
observability

Hướng nghiên cứu

Bắt đầu tại azure/ai/projects/telemetry/_responses_instrumentor.py, ở _append_to_message_attribute, rồi so sánh cách xử lý span với _set_span_attribute_safe và các wrapper luồng dữ liệu. Tái hiện lỗi bằng sampler ALWAYS_OFF của OpenTelemetry và một lệnh gọi responses.create không truyền luồng. Hoàn tất khi span không được ghi nhận không còn gây lỗi sau khi phản hồi thành công và phản hồi vẫn hoạt động.

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

Mô tả

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.

Ngôn ngữ chính
Python
Star
5.6k
Fork
3.4k
Merge trung bình
2 ngày 23 giờ
Pull request đã merge (30 ngày)
184

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

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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 Azure/azure-sdk-for-python

Tất cả issue của Azure/azure-sdk-for-python

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.