Skip to content

Commit de4e46f

Browse files
committed
fix(openai): mark interrupted agents and truncated multi-choice streams as errors
1 parent 313a634 commit de4e46f

4 files changed

Lines changed: 84 additions & 19 deletions

File tree

‎python/fi_instrumentation/instrumentation/_tracers.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,20 @@ def start_as_current_span(
203203
record_exception=record_exception,
204204
set_status_on_exception=set_status_on_exception,
205205
) as current_span:
206-
yield cast(FiSpan, current_span)
206+
try:
207+
yield cast(FiSpan, current_span)
208+
except BaseException as error:
209+
# OTel use_span handles Exception, but excludes cancellation and
210+
# other local aborts. Record those before the span ends, without
211+
# duplicating ordinary exceptions or bypassing FiSpan's privacy.
212+
if not isinstance(error, Exception) and span.is_recording():
213+
if record_exception:
214+
span.record_exception(error, escaped=True)
215+
if set_status_on_exception:
216+
span.set_status(Status(
217+
StatusCode.ERROR, f"{type(error).__name__}: {error}"
218+
))
219+
raise
207220

208221
def start_span(
209222
self,

‎python/frameworks/openai/traceai_openai/_request.py‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
FiSpanKindValues,
1212
SpanAttributes,
1313
)
14+
from openai import Omit
1415
from openai._legacy_response import LegacyAPIResponse
1516
from openai.types.create_embedding_response import CreateEmbeddingResponse
1617
from opentelemetry import context as context_api
@@ -222,14 +223,23 @@ def _finalize_response(
222223
cast_to: type,
223224
request_parameters: Mapping[str, Any],
224225
instance: Any = None,
226+
request_options: Any = None,
225227
) -> Any:
226228
"""
227229
Monkey-patch the response object to trace the stream, or finish tracing if the response is
228230
not a stream.
229231
"""
230232
response_accumulator_factory = self._response_accumulator_factories.get(cast_to)
233+
accumulator_parameters = request_parameters
234+
if cast_to is self._openai.types.chat.ChatCompletion and request_options is not None:
235+
# Validate the on-wire choice count without expanding request metadata
236+
# or passing arbitrary extra_body fields into response extraction.
237+
_, effective_parameters = _parse_request_args(
238+
(cast_to, request_options), include_extra_body=True,
239+
)
240+
accumulator_parameters = {**request_parameters, "n": effective_parameters.get("n", 1)}
231241
response_accumulator = (
232-
response_accumulator_factory(request_parameters)
242+
response_accumulator_factory(accumulator_parameters)
233243
if response_accumulator_factory
234244
else None
235245
)
@@ -241,7 +251,7 @@ def _finalize_response(
241251
extractor = _ResponseAttributesExtractor(self._openai, self._config, provider=provider)
242252
if cast_to is self._openai.types.chat.ChatCompletion:
243253
response_accumulator = _DeepSeekChatAccumulator(
244-
request_parameters=request_parameters,
254+
request_parameters=accumulator_parameters,
245255
chat_completion_type=cast_to,
246256
response_attributes_extractor=extractor,
247257
)
@@ -416,6 +426,7 @@ def __call__(
416426
cast_to=cast_to,
417427
request_parameters=request_parameters,
418428
instance=instance,
429+
request_options=args[1],
419430
)
420431
except BaseException as e:
421432
with_span.add_secrets(request_credentials(instance, getattr(e, "request", None), {}))
@@ -500,6 +511,7 @@ async def __call__(
500511
cast_to=cast_to,
501512
request_parameters=request_parameters,
502513
instance=instance,
514+
request_options=args[1],
503515
)
504516
except Exception:
505517
logger.exception(
@@ -509,19 +521,26 @@ async def __call__(
509521
return response
510522

511523

512-
def _parse_request_args(args: Tuple[type, Any]) -> Tuple[type, Mapping[str, Any]]:
524+
def _parse_request_args(
525+
args: Tuple[type, Any], *, include_extra_body: bool = False,
526+
) -> Tuple[type, Mapping[str, Any]]:
513527
# We don't use `signature(request).bind()` because `request` could have been monkey-patched
514528
# (incorrectly) by others and the signature at runtime may not match the original.
515529
# The targeted signature of `request` is here:
516530
# https://github.com/openai/openai-python/blob/f1c7d714914e3321ca2e72839fe2d132a8646e7f/src/openai/_base_client.py#L846-L847 # noqa: E501
517531
cast_to: type = args[0]
518-
request_parameters: Mapping[str, Any] = (
519-
json_data
520-
# See https://github.com/openai/openai-python/blob/f1c7d714914e3321ca2e72839fe2d132a8646e7f/src/openai/_models.py#L427 # noqa: E501
521-
if hasattr(args[1], "json_data")
522-
and isinstance(json_data := args[1].json_data, Mapping)
523-
else {}
524-
)
532+
json_data = getattr(args[1], "json_data", None)
533+
if include_extra_body and (extra_json := getattr(args[1], "extra_json", None)) is not None:
534+
# Mirror the SDK's shallow extra_body merge without mutating its options.
535+
# Opt in only for completion validation; exported metadata stays unchanged.
536+
if json_data is None:
537+
json_data = extra_json
538+
elif isinstance(json_data, Mapping) and isinstance(extra_json, Mapping):
539+
json_data = {
540+
key: value for key, value in {**json_data, **extra_json}.items()
541+
if not isinstance(value, Omit)
542+
}
543+
request_parameters: Mapping[str, Any] = json_data if isinstance(json_data, Mapping) else {}
525544
# FIXME: Because request parameters is just a Mapping, it can contain any value as long as it
526545
# serializes correctly in an HTTP request body. For example, Enum values may be present if a
527546
# third-party library puts them there. Enums can turn into their intended string values via

‎python/frameworks/openai/traceai_openai/_response_accumulator.py‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ def process_chunk(self, chunk: Any) -> None:
217217
# requested-model convention for streams; only aggregate message fields.
218218
self._values += {
219219
"choices": [
220-
{"index": choice["index"], "message": choice.get("delta") or {}}
220+
{"index": choice["index"], "message": choice.get("delta") or {},
221+
"finish_reason": choice.get("finish_reason")}
221222
for choice in raw_chunk.get("choices", [])
222223
if choice.get("index") is not None
223224
]
@@ -270,7 +271,28 @@ def get_attributes(self) -> Iterator[Tuple[str, AttributeValue]]:
270271
output_value = safe_json_dumps(messages)
271272
yield SpanAttributes.OUTPUT_VALUE, output_value
272273

273-
def get_status(self) -> Optional[trace_api.Status]:
274+
def get_status(self, *, exhausted: bool = False) -> Optional[trace_api.Status]:
275+
# Closing early is a normal caller action. Only judge a server-ended
276+
# stream when at least one choice proves the provider emits finish reasons.
277+
if not exhausted:
278+
return None
279+
choices = (self._result() or {}).get("choices", [])
280+
finished = {
281+
choice["index"] for choice in choices
282+
if choice.get("finish_reason") is not None
283+
}
284+
if not finished:
285+
return None
286+
expected = self._request_parameters.get("n", 1)
287+
if not isinstance(expected, int) or expected < 1:
288+
expected = 1
289+
# Check requested indices, not the count of returned choices. The indexed
290+
# accumulator retains reasons across null deltas and usage-only chunks.
291+
if any(index not in finished for index in range(expected)):
292+
return trace_api.Status(
293+
trace_api.StatusCode.ERROR,
294+
"Stream ended before all choices finished",
295+
)
274296
return None
275297

276298
def get_extra_attributes(self) -> Iterator[Tuple[str, AttributeValue]]:

‎python/frameworks/openai/traceai_openai/_stream.py‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from opentelemetry import trace as trace_api
88
from opentelemetry.util.types import AttributeValue
99
from traceai_openai._response_accumulator import (
10+
_ChatCompletionAccumulator,
1011
_ResponsesAccumulator,
1112
_raw_response_status,
1213
_response_status,
@@ -427,7 +428,7 @@ def __next__(self) -> Any:
427428
except StopIteration:
428429
if not self._self_is_finished:
429430
status = trace_api.Status(status_code=trace_api.StatusCode.OK)
430-
self._finish_tracing(status=status)
431+
self._finish_tracing(status=status, exhausted=True)
431432
raise
432433
except BaseException as exception:
433434
if not self._self_is_finished:
@@ -458,12 +459,17 @@ def _process_chunk(self, chunk: Any) -> None:
458459
def _finish_tracing(
459460
self,
460461
status: Optional[trace_api.Status] = None,
462+
*,
463+
exhausted: bool = False,
461464
) -> None:
462465
if self._self_is_finished:
463466
return
464467
if status is None or status.status_code is not trace_api.StatusCode.ERROR:
465-
get_status = getattr(self._self_response_accumulator, "get_status", None)
466-
if get_status is not None:
468+
accumulator = self._self_response_accumulator
469+
get_status = getattr(accumulator, "get_status", None)
470+
if isinstance(accumulator, _ChatCompletionAccumulator):
471+
status = accumulator.get_status(exhausted=exhausted) or status
472+
elif get_status is not None:
467473
status = get_status() or status
468474
_finish_tracing(
469475
status=status,
@@ -544,7 +550,7 @@ async def __anext__(self):
544550
except StopAsyncIteration:
545551
if not self._self_is_finished:
546552
status = trace_api.Status(status_code=trace_api.StatusCode.OK)
547-
self._finish_tracing(status=status)
553+
self._finish_tracing(status=status, exhausted=True)
548554
raise
549555
except BaseException as exception:
550556
if not self._self_is_finished:
@@ -575,12 +581,17 @@ def _process_chunk(self, chunk: Any) -> None:
575581
def _finish_tracing(
576582
self,
577583
status: Optional[trace_api.Status] = None,
584+
*,
585+
exhausted: bool = False,
578586
) -> None:
579587
if self._self_is_finished:
580588
return
581589
if status is None or status.status_code is not trace_api.StatusCode.ERROR:
582-
get_status = getattr(self._self_response_accumulator, "get_status", None)
583-
if get_status is not None:
590+
accumulator = self._self_response_accumulator
591+
get_status = getattr(accumulator, "get_status", None)
592+
if isinstance(accumulator, _ChatCompletionAccumulator):
593+
status = accumulator.get_status(exhausted=exhausted) or status
594+
elif get_status is not None:
584595
status = get_status() or status
585596
_finish_tracing(
586597
status=status,

0 commit comments

Comments
 (0)