Refactor tool_calls handling in iter_response and async_iter_response functions for improved readability and logic consistency

This commit is contained in:
hlohaus
2025-09-07 01:44:03 +02:00
parent bc77a9226e
commit 24ebab640c
2 changed files with 6 additions and 4 deletions

View File

@@ -143,7 +143,9 @@ def iter_response(
content = filter_json(content)
chat_completion = ChatCompletion.model_construct(
content, finish_reason, completion_id, int(time.time()), usage=usage,
**filter_none(tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]) if tool_calls is not None else {},
**(filter_none(
tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]
) if tool_calls is not None else {}),
conversation=None if conversation is None else conversation.get_dict(),
reasoning=reasoning if reasoning else None
)
@@ -246,9 +248,9 @@ async def async_iter_response(
content = filter_json(content)
chat_completion = ChatCompletion.model_construct(
content, finish_reason, completion_id, int(time.time()), usage=usage,
**filter_none(
**(filter_none(
tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]
) if tool_calls is not None else {},
) if tool_calls else {}),
conversation=conversation,
reasoning=reasoning if reasoning else None
)

View File

@@ -279,7 +279,7 @@ class ChatCompletionDelta(BaseModel):
def model_construct(cls, content: Optional[str]):
if isinstance(content, Reasoning):
return super().model_construct(role="assistant", content=None, reasoning=str(content))
elif isinstance(content, ToolCalls):
elif isinstance(content, ToolCalls) and content.get_list():
return super().model_construct(role="assistant", content=None, tool_calls=[
ToolCallModel.model_construct(**tool_call) for tool_call in content.get_list()
])