Repository navigation
Conversation
ch-z-hc
marked this pull request as ready for review
October 8, 2026 13:34
chrikrah
approved these changes
Oct 12, 2026
chrikrah
left a comment
There was a problem hiding this comment.
@ch-z-hc refusal text now lands on the generation in all four sync/async and stream/no-stream cases, and 6579cf1 is ready to go in as it stands. Your four new cases fail against main's openai.py.
$ pytest -q tests/unit/test_openai.py tests/unit/test_openai_prompt_extraction.py # uv sync --locked, Python 3.12, openai 2.29.0
64 passed # head 6579cf1
4 failed, 60 passed # langfuse/openai.py from bf11ec1, your tests kept
FAILED tests/unit/test_openai.py::test_chat_completion_captures_refusal[False-False]
FAILED tests/unit/test_openai.py::test_chat_completion_captures_refusal[True-True]
$ pytest -q tests/unit/test_openai.py # prepare-v5-release edf44eb + this diff, Python 3.14.7, openai 2.29.0
41 passed # with the change
4 failed (-k refusal) # v5's own openai.py
# not run: the e2e suites against a Langfuse server, a live OpenAI call
non-blocking: a streamed refusal now logs a {role, refusal, content} dict. Other text streams log a bare string. I'd keep the dict. The non-streamed path records the same dict, so a filter on output.refusal finds both.
@hassiebp the diff also applies onto prepare-v5-release with no conflict in openai.py, and the refusal cases pass there too. Should it land on main now, or ride along with v5?
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Preserves OpenAI Chat Completions refusal text in generation output, including streamed responses.
When OpenAI returns
content=Noneand arefusal, the SDK consumer receives the refusal but the Langfuse trace loses it. Non-streaming output only containsroleandcontent; refusal-only streams have no recorded output. This makes refused requests indistinguishable from empty responses when inspecting traces or evaluating model behavior.The patch copies non-null
refusalinto chat output and accumulates streamed refusal deltas. Existing output shapes for responses without a refusal are unchanged. The field is part of the OpenAI Chat Completions streaming contract.The regression test exercises the actual OpenAI SDK with HTTPX MockTransport and the in-memory exporter, covering sync/async clients and streaming/non-streaming responses. It verifies both the application-visible response and recorded output/usage. Before the fix, all four new cases fail; the other 37 tests in
test_openai.pypass.Type of change
Verification
Python 3.12, locked dependencies:
test_shutdown_and_flush,test_span_metadata_updates_in_async_context). No test was disabled or weakened for this patch.ruff format --check .reports an existing formatting issue in unchangedtests/unit/test_media.py; left outside this patch.pythonsubprocess and POSIX-path failures. The relevant failures also reproduce on the unmodified base. The Linux run above passes the complete unit suite.Checklist
code_review.md..env.templateif needed. (Not needed: no examples or configuration become stale.)AI assistance: Codex assisted with investigation, implementation, tests and diff self-review. This is not a claim of independent human review.
The refusal fix looks sound, but the new test must satisfy the repository’s import rule before merging.
Summary
Preserves OpenAI refusal text in completed and streamed chat traces.
httpximport to the module’s import block to satisfy the repository rule.Reviews (1) · Last reviewed commit: "fix(openai): preserve refusal text in tr..." · Reviewed by Greptile