Skip to content

Commit ae3b67a

Browse files
andrewm4894claude
andcommitted
test: add test for None model fallback in non-streaming
Verifies that non-streaming returns None (not "unknown") when model is not available in kwargs or response, matching original behavior. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent cc401a0 commit ae3b67a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

posthog/test/ai/openai/test_openai.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,52 @@ def test_non_streaming_responses_api_extracts_model_from_response(mock_client):
19441944
assert props["$ai_model"] == "gpt-4o-mini-stored"
19451945

19461946

1947+
def test_non_streaming_returns_none_when_no_model(mock_client):
1948+
"""Test that non-streaming returns None (not 'unknown') when model is not available anywhere."""
1949+
# Create a response without model attribute using real OpenAI types
1950+
mock_response = ChatCompletion(
1951+
id="test",
1952+
model="", # Will be removed below
1953+
object="chat.completion",
1954+
created=int(time.time()),
1955+
choices=[
1956+
Choice(
1957+
finish_reason="stop",
1958+
index=0,
1959+
message=ChatCompletionMessage(
1960+
content="Test response",
1961+
role="assistant",
1962+
),
1963+
)
1964+
],
1965+
usage=CompletionUsage(
1966+
completion_tokens=5,
1967+
prompt_tokens=10,
1968+
total_tokens=15,
1969+
),
1970+
)
1971+
# Remove model attribute to simulate missing model
1972+
object.__delattr__(mock_response, "model")
1973+
1974+
with patch(
1975+
"openai.resources.chat.completions.Completions.create",
1976+
return_value=mock_response,
1977+
):
1978+
client = OpenAI(api_key="test-key", posthog_client=mock_client)
1979+
1980+
# Note: NOT passing model in kwargs and response has no model
1981+
client.chat.completions.create(
1982+
messages=[{"role": "user", "content": "Hello"}],
1983+
posthog_distinct_id="test-id",
1984+
)
1985+
1986+
call_args = mock_client.capture.call_args[1]
1987+
props = call_args["properties"]
1988+
1989+
# Should be None, NOT "unknown" (to avoid incorrect cost matching)
1990+
assert props["$ai_model"] is None
1991+
1992+
19471993
def test_streaming_falls_back_to_unknown_when_no_model(mock_client):
19481994
"""Test that streaming falls back to 'unknown' when model is not available anywhere."""
19491995
from unittest.mock import MagicMock

0 commit comments

Comments
 (0)