Skip to content

server: Support chat_template_kwargs for /v1/messages - #22154

Merged
aldehir merged 1 commit into
ggml-org:masterfrom
Soreepeong:anthropic-oai-conv-chat-template-kwargs
Apr 23, 2026
Merged

aldehir merged 1 commit into
ggml-org:masterfrom
Soreepeong:anthropic-oai-conv-chat-template-kwargs

Conversation

@Soreepeong

@Soreepeong Soreepeong commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor

Overview

This PR passthroughs chat_template_kwargs when the feature is invoked using Anthropic compatible endpoint.

OpenAI compatible endpoint /v1/completions will handle chat_template_kwargs, but Anthropic compatible endpoint /v1/messages does not. As it is a nonstandard option in OAI anyway, I thought it's fine to add nonstandard feature to Anthropic handler too.

Additional information

Use case

I am using a custom proxy application that transforms model name suffix to parameter presets, such as /think suffix will add {"chat_template_kwargs": true} to the completions/messages request body.

Testing

The model is configured to have thinking disabled.

Command

curl -v http://127.0.0.1:12345/v1/messages ^
  -H "anthropic-version: 2023-06-01" ^
  -H "content-type: application/json" ^
  --data-raw "{\"model\": \"Qwen3.6-35B-A3B\", \"chat_template_kwargs\": {\"enable_thinking\": true}, \"messages\": [{\"role\": \"user\", \"content\": \"What is 1 + 1?\"}]}"

Before this PR

{"id":"chatcmpl-AzD7ukLinv1EGcbg3fX6gV7Sy1AH54tq","type":"message","role":"assistant","content":[{"type":"text","text":"1 + 1 equals **2**."}],"model":"Qwen3.6-35B-A3B","stop_reason":"end_turn","stop_sequence":null,"usage":{"cache_read_input_tokens":0,"input_tokens":20,"output_tokens":9}}

After this PR

{"id":"chatcmpl-O9T75TOmt4gRo5NnzlaVLQcIVuZAOopd","type":"message","role":"assistant","content":[{"type":"thinking","thinking":"Here's a thinking process:\n\n1.  **Analyze User Input:** The user asks \"What is 1 + 1?\"\n2.  **Identify Core Task:** This is a basic arithmetic question.\n3.  **Calculate Result:** 1 + 1 = 2.\n4.  **Formulate Response:** State the answer clearly and concisely. \"1 + 1 equals 2.\"\n5.  **Check for Edge Cases/Context:** None. It's a straightforward math question with no tricks or additional context needed.\n6.  **Final Output Generation:** Provide the direct answer.✅\n","signature":""},{"type":"text","text":"1 + 1 equals **2**."}],"model":"Qwen3.6-35B-A3B","stop_reason":"end_turn","stop_sequence":null,"usage":{"cache_read_input_tokens":0,"input_tokens":18,"output_tokens":144}}*

Requirements

@Soreepeong
Soreepeong marked this pull request as ready for review April 20, 2026 07:07
@Soreepeong
Soreepeong requested a review from a team as a code owner April 20, 2026 07:07
@Soreepeong Soreepeong changed the title Support chat_template_kwargs for /v1/messages server: Support chat_template_kwargs for /v1/messages Apr 20, 2026
@Soreepeong

Copy link
Copy Markdown
Contributor Author

I noticed that the function got moved to server-chat.cpp. Should I rebase and make this PR edit that file instead?

@aldehir

aldehir commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Yes, the conversion functions were recently moved.

@Soreepeong
Soreepeong force-pushed the anthropic-oai-conv-chat-template-kwargs branch from 9a4be20 to bbe203c Compare April 23, 2026 03:55
@Soreepeong
Soreepeong requested review from aldehir and ngxson April 23, 2026 03:55
@ggerganov ggerganov added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Apr 23, 2026
@ggerganov

Copy link
Copy Markdown
Member

Btw, a word of advice is that Claude Code is probably the worst option to use with local models. Such hacks will only do so much and eventually you would have to switch to something else.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

Funny thing is, this also applies to Claude Opus itself. A simple bash_tool in a well structured linear context already delivers the best of any model

@aldehir
aldehir merged commit 185cbff into ggml-org:master Apr 23, 2026
47 of 49 checks passed
@aldehir

aldehir commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

I agree, vendor specific tooling (Codex, Claude Code) are not good tools for local inference. They often break on updates, and appear to progressively offload more tools to the backend which is unsupported/out of scope in llama.cpp.

The one benefit of these endpoints is they natively support sending back reasoning tokens, which has no real standard in the chat completions API. Although, it's probably best to use the Responses API for that.

@Soreepeong
Soreepeong deleted the anthropic-oai-conv-chat-template-kwargs branch April 23, 2026 18:42
IntelNav pushed a commit to IntelNav/llama.cpp that referenced this pull request Apr 29, 2026
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
baramofme pushed a commit to baramofme/llama-cpp-turboquant that referenced this pull request May 23, 2026
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants