Skip to content

fix(openai): preserve audio data in provider responses - #2029

Merged
hassiebp merged 1 commit into
langfuse:mainfrom
HuaTNA:fix-openai-audio-response-mutation
Oct 21, 2026
Merged

hassiebp merged 1 commit into
langfuse:mainfrom
HuaTNA:fix-openai-audio-response-mutation

Conversation

@HuaTNA

@HuaTNA HuaTNA commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes langfuse/langfuse#19094.

OpenAI chat-completion audio tracing replaces the provider's audio.data string with a LangfuseMedia object because extraction modifies the original audio object's __dict__. The response returned to application code then fails base64 decoding or JSON serialization.

Copy the audio dictionary before converting its data for tracing. The provider response stays unchanged, while the exported generation still contains the media reference and transcript. The source change is one line; a single regression exercises the instrumented public chat-completion call with an actual OpenAI SDK response object and the shared in-memory exporter. Provider HTTP and media uploads are mocked.

Type of change

  • Bug fix

Verification

Locked environment on macOS arm64, Python 3.13.13, OpenAI 2.29.0, Pydantic 2.14.0 and OpenTelemetry SDK 1.45.0:

uv sync --locked
uv run --frozen pytest tests/unit/test_openai.py::test_chat_completion_audio_preserves_provider_response -q
uv run --frozen pytest tests/unit/test_openai.py tests/unit/test_media.py tests/unit/test_openai_prompt_extraction.py -q
LANGFUSE_PUBLIC_KEY=unit-test-public-key LANGFUSE_SECRET_KEY=unit-test-secret-key LANGFUSE_BASE_URL=http://127.0.0.1:3000 uv run --frozen pytest -n auto --dist worksteal tests/unit
uv run --frozen ruff check .
uv run --frozen mypy langfuse --no-error-summary
uv run --frozen ruff format --check langfuse/openai.py tests/unit/test_openai.py
uv run pre-commit run --files langfuse/openai.py tests/unit/test_openai.py
git diff --check

The regression fails on unchanged main with PydanticSerializationError and passes after the fix. Related tests pass 81/81. The full unit suite passes 949 tests, with 2 skipped. Repository lint, mypy, changed-file formatting, configured hooks and diff checks pass.

The first full-suite attempt had setup errors from missing dummy Langfuse credentials and sandbox-blocked loopback HTTP fixtures. The passing rerun supplied dummy local configuration and allowed loopback fixtures. No real Langfuse server, provider credentials or paid calls were used.

Whole-repository ruff format --check . reports an existing formatting issue in untouched langfuse/_client/resource_manager.py. Its bytes match main, and formatting a copy from main fails identically; this PR leaves it unchanged. No e2e/live-provider tests, actual audio generation/playback, persisted backend behavior, other Python versions or OSes were tested.

Found through code analysis and reproduced locally with Codex assistance. Codex also assisted with implementation and test execution; this does not claim production usage or independent human review.

Checklist

  • I self-reviewed the diff using code_review.md.
  • I added or updated tests for behavior changes.
  • I checked whether docs, examples, or .env.template needed changes; this preserves the existing response contract.
  • I did not hand-edit generated files.
  • I did not commit secrets or credentials.

RetriggerView in GreptileConfidence Score: 5/5

This PR appears safe to merge.

Summary

Copies the audio dictionary before replacing its data for tracing. This keeps the provider response unchanged while preserving the recorded media reference and transcript.

  • OpenAI audio responses stay unchanged while Langfuse records them.

Reviews (1) · Last reviewed commit: "fix(openai): preserve audio data in prov..." · Reviewed by Greptile

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hassiebp

Copy link
Copy Markdown
Collaborator

Thanks a lot for your contribution, @HuaTNA !

@hassiebp
hassiebp merged commit 0658c20 into langfuse:main Oct 21, 2026
1 check was pending
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.

bug(sdk-python): OpenAI audio tracing mutates returned response data

3 participants