Skip to content

fix(ai): take the served tier of a Responses stream from the terminal response - #1296

Open
utsxvrai wants to merge 1 commit into
PostHog:mainfrom
utsxvrai:fix/ai-responses-stream-service-tier
Open

utsxvrai wants to merge 1 commit into
PostHog:mainfrom
utsxvrai:fix/ai-responses-stream-service-tier

Conversation

@utsxvrai

@utsxvrai utsxvrai commented Nov 24, 2026 •

Copy link
Copy Markdown

💡 Motivation and Context

A streamed OpenAI Responses call reports the wrong $ai_service_tier. In a real stream, response.created and response.in_progress echo the requested tier and only the terminal response names the tier that served the request. In the recorded posthog-js cassette packages/ai/harness/fixtures/openai-responses-stream.json the values are auto, then auto, then default.

_ResponsesStreamState._record_response_metadata (posthog/ai/openai/_streaming.py) kept the first non-null value it saw. So every streamed Responses call that asked for service_tier="auto" (or left it to the project default) captured $ai_service_tier: "auto", and $ai_model_parameters.service_tier was "auto" too. Neither is a served tier. Non-streamed calls already read the tier from the final response, and @posthog/ai's OpenAIResponsesStreamAccumulator keeps the latest non-null value.

The state now keeps the latest non-null tier, which is the one on the terminal response. Chat Completions streams aren't changed, because their chunks all carry the served tier.

Repro (before the fix):

client = posthog.ai.openai.OpenAI(posthog_client=ph)
for _ in client.responses.create(model="gpt-4o-mini", input="Hi", service_tier="auto", stream=True):
    pass
# before: $ai_service_tier == "auto"
# after:  $ai_service_tier == "default"

💚 How did you test it?

  • New tests test_streaming_responses_reports_tier_from_terminal_response and its async twin go through the wrapped responses.create(stream=True) with the created → in_progress → completed sequence (auto, auto, default). Both fail on main (assert 'auto' == 'default') and pass with the fix.
  • pytest posthog/test/ai/openai: 216 passed, 1 skipped.
  • pytest posthog/test/ai: 1927 passed, 15 skipped.
  • ruff format --check ., ruff check ., the mypy baseline filter and python -W error -c "import posthog" are all clean.
  • No live OpenAI call was made. The event sequence comes from the recorded cassette mentioned above.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file (.sampo/changesets/responses-stream-served-service-tier.md, a pypi/posthog: patch entry written by hand in the same format as the existing changesets)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • An AI coding agent (Claude Code) found the bug under my direction while comparing the Python stream state with @posthog/ai's Responses accumulator and with the recorded OpenAI Responses cassette. The agent wrote the failing tests first, then the fix. I read the full diff after the PR was opened.
  • The fix is limited to the Responses stream. Chat Completions chunks all carry the same served tier in the recorded chat cassettes, so that path stays as it is.

… response

response.created and response.in_progress echo the requested service
tier ("auto"), and only the terminal response names the tier that
served the request. The stream state kept the first value it saw, so
$ai_service_tier was "auto" for streamed Responses calls. Keep the
latest non-null tier instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant