Attribute failed `FallbackModel` spans to the failing model, not the `fallback:` wrapper

Merged
ST
4mo ago
+152-2

Problem

FallbackModel opens the chat span under its composite identity, for example:

gen_ai.system = "fallback:google-cloud,openai"
gen_ai.request.model = "fallback:gemini-...,gpt-..."

The successful request path replaces those attributes with the inner model that served the request. A terminal non-fallback-eligible error instead propagated immediately, leaving the failed call attributed to the synthetic fallback:... provider/model.

This affects both the normal fallback chain and a pinned continuation resumed on a specific inner model, in streaming and non-streaming requests.

Fix

Resolve the span to the inner model that produced a terminal non-fallback-eligible error before re-raising it in all four dispatch paths:

  • request fallback chain
  • request_stream fallback chain
  • pinned continuation request
  • pinned continuation request_stream

Fallback-eligible errors still continue to the next model without resolving the composite identity. If every model fails with a fallback-eligible error, the existing composite fallback:... attribution remains unchanged.

Each attempt initializes its prepared parameters from the original request, then replaces them when prepare_request succeeds. This means terminal request errors record the inner model's prepared parameters, while an error raised by prepare_request itself remains safe and cannot reuse stale parameters from an earlier attempt.

Tests

The focused telemetry tests cover:

  • a terminal error after an earlier fallback, for request and stream opening
  • a terminal error from a freshly instrumented pinned continuation, for request and stream opening
  • prepared parameters from the terminal model
  • an error raised directly by prepare_request

Validation performed locally:

  • uv run pytest tests/models/test_fallback.py — 89 passed after merging current main
  • Ruff formatting and linting on the changed files
  • Pyright on the changed files — 0 errors

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.