Skip to content

Commit 9f4b4fb

Browse files
ericapisaniclaude
andcommitted
fix(openai): Use realistic Choice objects in manual output counting test
The test used MagicMock(message="one") where message was a plain string, but the real OpenAI API returns Choice objects with message.content. The counting code checks hasattr(choice.message, "content"), which failed on strings, so manual token counting was never exercised. Use real Choice and ChatCompletionMessage objects and fix the expected output_tokens. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d867724 commit 9f4b4fb

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

tests/integrations/openai/test_openai.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,12 +2105,24 @@ def count_tokens(msg):
21052105
response.usage.prompt_tokens = 20
21062106
response.usage.total_tokens = 20
21072107
response.choices = [
2108-
mock.MagicMock(message="one"),
2109-
mock.MagicMock(message="two"),
2110-
mock.MagicMock(message="three"),
2108+
Choice(
2109+
index=0,
2110+
finish_reason="stop",
2111+
message=ChatCompletionMessage(role="assistant", content="one"),
2112+
),
2113+
Choice(
2114+
index=1,
2115+
finish_reason="stop",
2116+
message=ChatCompletionMessage(role="assistant", content="two"),
2117+
),
2118+
Choice(
2119+
index=2,
2120+
finish_reason="stop",
2121+
message=ChatCompletionMessage(role="assistant", content="three"),
2122+
),
21112123
]
21122124
messages = []
2113-
streaming_message_responses = []
2125+
streaming_message_responses = None
21142126

21152127
with mock.patch(
21162128
"sentry_sdk.integrations.openai.record_token_usage"
@@ -2127,7 +2139,7 @@ def count_tokens(msg):
21272139
span,
21282140
input_tokens=20,
21292141
input_tokens_cached=None,
2130-
output_tokens=None,
2142+
output_tokens=11,
21312143
output_tokens_reasoning=None,
21322144
total_tokens=20,
21332145
)

0 commit comments

Comments
 (0)