Skip to content

fix(parsing): don't default to an empty array #2106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/openai/lib/_parsing/_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def parse_chat_completion(
response_format=response_format,
message=message,
),
"tool_calls": tool_calls,
"tool_calls": tool_calls if tool_calls else None,
},
},
)
Expand Down
22 changes: 11 additions & 11 deletions tests/lib/chat/test_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_parse_nothing(client: OpenAI, respx_mock: MockRouter, monkeypatch: pyte
parsed=None,
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
],
Expand Down Expand Up @@ -132,7 +132,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=65.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
],
Expand Down Expand Up @@ -201,7 +201,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=65.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
],
Expand Down Expand Up @@ -272,7 +272,7 @@ class ColorDetection(BaseModel):
parsed=ColorDetection(color=<Color.RED: 'red'>, hex_color_code='#FF0000'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
"""
Expand Down Expand Up @@ -321,7 +321,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=64.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
),
ParsedChoice[Location](
Expand All @@ -335,7 +335,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=65.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
),
ParsedChoice[Location](
Expand All @@ -349,7 +349,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=63.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -399,7 +399,7 @@ class CalendarEvent:
parsed=CalendarEvent(name='Science Fair', date='Friday', participants=['Alice', 'Bob']),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
],
Expand Down Expand Up @@ -571,7 +571,7 @@ class Location(BaseModel):
parsed=None,
refusal="I'm very sorry, but I can't assist with that.",
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -855,7 +855,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=58.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
],
Expand Down Expand Up @@ -930,7 +930,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=65.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
],
Expand Down
20 changes: 10 additions & 10 deletions tests/lib/chat/test_completions_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_parse_nothing(client: OpenAI, respx_mock: MockRouter, monkeypatch: pyte
parsed=None,
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -147,7 +147,7 @@ def on_event(stream: ChatCompletionStream[Location], event: ChatCompletionStream
parsed=Location(city='San Francisco', temperature=61.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
],
Expand Down Expand Up @@ -324,7 +324,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=65.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
),
ParsedChoice[Location](
Expand All @@ -338,7 +338,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=61.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
),
ParsedChoice[Location](
Expand All @@ -352,7 +352,7 @@ class Location(BaseModel):
parsed=Location(city='San Francisco', temperature=59.0, units='f'),
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -427,7 +427,7 @@ class Location(BaseModel):
parsed=None,
refusal="I'm sorry, I can't assist with that request.",
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -501,7 +501,7 @@ def test_content_logprobs_events(client: OpenAI, respx_mock: MockRouter, monkeyp
parsed=None,
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -612,7 +612,7 @@ class Location(BaseModel):
parsed=None,
refusal="I'm very sorry, but I can't assist with that.",
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -925,7 +925,7 @@ def test_non_pydantic_response_format(client: OpenAI, respx_mock: MockRouter, mo
parsed=None,
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down Expand Up @@ -1040,7 +1040,7 @@ def streamer(client: OpenAI) -> Iterator[ChatCompletionChunk]:
parsed=None,
refusal=None,
role='assistant',
tool_calls=[]
tool_calls=None
)
)
]
Expand Down