From b6a26da50cb1cfcac0585d1909b31304540cc61f Mon Sep 17 00:00:00 2001 From: kingbri <8082010+kingbri1@users.noreply.github.com> Date: Fri, 4 Jul 2025 15:02:49 -0400 Subject: [PATCH] API: Fix tool call serialization To render in the template, tool call start tokens needed to have less checks and remove the line to convert message.tool_calls to a dict since that breaks the rest of the chain by disconnecting the types. model_dump on the message itself already accomplishes this. Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com> --- endpoints/OAI/types/chat_completion.py | 2 +- endpoints/OAI/utils/chat_completion.py | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/endpoints/OAI/types/chat_completion.py b/endpoints/OAI/types/chat_completion.py index 561d9bc..405a609 100644 --- a/endpoints/OAI/types/chat_completion.py +++ b/endpoints/OAI/types/chat_completion.py @@ -56,7 +56,7 @@ class ChatCompletionStreamChoice(BaseModel): # Inherited from common request class ChatCompletionRequest(CommonCompletionRequest): - messages: List[ChatCompletionMessage] = Field(default_factory=list) + messages: List[ChatCompletionMessage] prompt_template: Optional[str] = None add_generation_prompt: Optional[bool] = True template_vars: Optional[dict] = Field( diff --git a/endpoints/OAI/utils/chat_completion.py b/endpoints/OAI/utils/chat_completion.py index f9a556f..bfed45a 100644 --- a/endpoints/OAI/utils/chat_completion.py +++ b/endpoints/OAI/utils/chat_completion.py @@ -207,12 +207,11 @@ async def _append_template_metadata(data: ChatCompletionRequest, template_vars: if isinstance(data.stop, str): data.stop = [data.stop] + template_metadata.stop_strings else: - data.stop += template_metadata.stop_strings + data.stop.extend(template_metadata.stop_strings) # Tool call start strings if template_metadata.tool_starts: - if data.tool_call_start is None: - data.tool_call_start = template_metadata.tool_starts + data.tool_call_start.extend(template_metadata.tool_starts) # Append to stop strings to halt for a tool call generation data.stop.extend(template_metadata.tool_starts) @@ -245,10 +244,6 @@ async def format_messages_with_template( if message.tool_calls: message.tool_calls_json = ToolCallProcessor.to_json(message.tool_calls) - # The tools variable is inspectable in the template, so - # store the list of dicts rather than the ToolCallProcessor object. - message.tool_calls = ToolCallProcessor.dump(message.tool_calls) - message_dicts.append(message.model_dump(exclude_none=True)) # Get all special tokens