fix(openai): omit absent tool_calls from chat responses (#43027)

Co-authored-by: ai-jz <ai-jz@users.noreply.github.com>
Co-authored-by: Xinyuan Tong <xinyuantong.cs@gmail.com>
This commit is contained in:
ai-jz
2026-10-13 20:56:58 +08:00
committed by GitHub
co-authored by ai-jz Xinyuan Tong
parent 8e37c6cf37
commit 27b68303d0
3 changed files with 66 additions and 1 deletions
@@ -1212,6 +1212,13 @@ class ChatMessage(BaseModel):
reasoning_content: Optional[str] = None
tool_calls: Optional[List[ToolCall]] = Field(default=None, examples=[None])
@model_serializer(mode="wrap")
def _serialize(self, handler):
data = handler(self)
if self.tool_calls is None:
data.pop("tool_calls", None)
return data
class ChatCompletionResponseChoice(BaseModel):
index: int
@@ -1286,6 +1293,13 @@ class ChatCompletionResponseStreamChoice(BaseModel):
] = None
matched_stop: Union[None, int, str] = None
@model_serializer(mode="wrap")
def _serialize(self, handler):
data = handler(self)
if self.delta.tool_calls is None:
data["delta"].pop("tool_calls", None)
return data
class ChatCompletionStreamResponse(BaseModel):
id: str