Skip to content

[serve] Keep assistant content when tool_calls is null or empty - #50707

Open
AabhasK wants to merge 2 commits into
huggingface:mainfrom
AabhasK:fix-serve-empty-tool-calls
Open

AabhasK wants to merge 2 commits into
huggingface:mainfrom
AabhasK:fix-serve-empty-tool-calls

Conversation

@AabhasK

@AabhasK AabhasK commented Nov 13, 2026 •

Copy link
Copy Markdown

🤗 Transformers CI

What does this PR do?

transformers serve treats an assistant message as a tool call whenever the tool_calls key is present, even when its value is null or [] (get_processor_inputs_from_messages in cli/serving/utils.py).

  • "tool_calls": null raises a TypeError, which surfaces as an HTTP 500. This is the shape message.model_dump() from the OpenAI client produces.
  • "tool_calls": [] silently drops the assistant's content from the prompt.

This PR checks that tool_calls is non-empty instead of checking that the key exists. It is a standalone PR, I did not find an existing issue for it.

Reproduction

Start the server:

transformers serve Qwen/Qwen2.5-0.5B-Instruct --port 8000

Then run:

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="unused")
model = "Qwen/Qwen2.5-0.5B-Instruct"

messages = [{"role": "user", "content": "My name is Aurelio. Say hello to me."}]
reply = client.chat.completions.create(model=model, messages=messages).choices[0].message

for tool_calls in (None, []):
    history = messages + [
        {"role": "assistant", "content": reply.content, "tool_calls": tool_calls},
        {"role": "user", "content": "What is my name?"},
    ]
    try:
        out = client.chat.completions.create(model=model, messages=history, temperature=0)
        print(f"tool_calls={tool_calls}: {out.usage.prompt_tokens} prompt tokens ->", out.choices[0].message.content)
    except Exception as e:
        print(f"tool_calls={tool_calls}: {type(e).__name__}: {e}")

Output on main:

tool_calls=None: InternalServerError: Error code: 500
tool_calls=[]: 55 prompt tokens -> Hello! My name is Qwen, and I am an artificial intelligence developed by Alibaba Cloud. How can I assist you today?

Output with this PR:

tool_calls=None: 74 prompt tokens -> Your name is Aurelio.
tool_calls=[]: 74 prompt tokens -> Your name is Aurelio.

Test

Added test_llm_empty_tool_calls_keeps_content to tests/cli/test_serve.py. It fails on main and passes with this PR.

Who can review?

@SunMarc @Rocketknight1

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant