vllm.entrypoints.anthropic.serving
¶
Anthropic Messages API serving handler.
Classes:
-
AnthropicServingMessages–Handler for Anthropic Messages API requests.
-
DisabledThinkingEffortResolver–Resolve and cache the effort used for Anthropic disabled thinking.
AnthropicServingMessages
¶
Bases: OpenAIServingChat
Handler for Anthropic Messages API requests.
Methods:
-
count_tokens–Implements Anthropic's messages.count_tokens endpoint.
-
create_messages–Messages API similar to Anthropic's API.
-
to_chat_completion_request–Convert Anthropic message format to OpenAI format.
_build_base_request(anthropic_request, openai_messages)
classmethod
¶
Build base ChatCompletionRequest.
_convert_block(block, role, content_parts, tool_calls, reasoning_parts, openai_messages)
classmethod
¶
Convert individual content block.
_convert_image_source_to_url(source)
staticmethod
¶
Convert an Anthropic image source to an OpenAI-compatible URL.
Anthropic supports two image source types: - base64: {"type": "base64", "media_type": "image/jpeg", "data": "..."} - url: {"type": "url", "url": "https://..."}
For base64 sources, this constructs a proper data URI that downstream processors (e.g. vLLM's media connector) can handle.
_convert_message_content(msg, openai_msg, openai_messages)
classmethod
¶
Convert complex message content blocks.
_convert_messages(messages, openai_messages, *, merge_inline_system=False)
classmethod
¶
Convert Anthropic messages to OpenAI format.
_convert_system_message(anthropic_request, openai_messages, *, merge_inline_system=False)
classmethod
¶
Convert Anthropic system message to OpenAI format.
_convert_tool_choice(anthropic_request, req)
classmethod
¶
Convert Anthropic tool_choice to OpenAI format.
_convert_tool_result_block(block, role, openai_messages, content_parts)
classmethod
¶
Convert tool_result block to OpenAI format.
_convert_tool_use_block(block, tool_calls)
classmethod
¶
Convert tool_use block to OpenAI function call format.
_convert_tools(anthropic_request, req)
classmethod
¶
Convert Anthropic tools to OpenAI format.
_convert_user_tool_result(block, openai_messages)
classmethod
¶
Convert user tool_result with text and image support.
_detect_merge_inline_system(chat_template)
staticmethod
¶
Auto-detect whether the chat template requires system-first ordering.
Renders a [system, user, system, user] conversation against the
template; if it raises (e.g. Qwen's loop.first guard), the
model needs inline system messages merged into the leading block.
_extract_system_text(msg)
classmethod
¶
Extract text from a system message, stripping billing headers.
_handle_output_config(req, anthropic_request)
classmethod
¶
Handle output configuration such as output format and effort.
_handle_streaming_options(req, anthropic_request)
classmethod
¶
Handle streaming configuration.
_handle_thinking(req, anthropic_request, disabled_thinking_effort='none')
classmethod
¶
Handle extended-thinking configuration.
display is intentionally ignored: suppressing reasoning would mark
it ended for structured outputs and drop it from multi-turn history.
_renderer_keeps_inline_system(online_renderer)
staticmethod
¶
Probe a non-jinja renderer (e.g. DeepSeek V4.1's encoder).
Inline system messages must render in order as distinct system turns, and a trailing one must still be followed by the generation prompt. DeepSeek V4 appends their text to the user turn, V3.2 puts it after the generation prompt, and Mistral moves it into the last user turn.
_resolve_tools(anthropic_request)
classmethod
¶
Apply tool_addition/tool_removal blocks in messages to tools.
Chat templates see a single tool list, so this resolves the tools in effect at the end of the conversation: a tool added by reference is no longer deferred, a tool added by value is declared, and a removed tool is dropped.
_should_merge_inline_system(online_renderer)
classmethod
¶
Probe the chat templates the renderer applies, not the CLI override.
Both the tool and non-tool templates are checked since they may differ.
count_tokens(request, raw_request=None)
async
¶
Implements Anthropic's messages.count_tokens endpoint.
create_messages(request, raw_request=None)
async
¶
Messages API similar to Anthropic's API.
See https://docs.anthropic.com/en/api/messages for the API specification. This API mimics the Anthropic messages API.
to_chat_completion_request(anthropic_request, *, merge_inline_system=False, disabled_thinking_effort='none')
classmethod
¶
Convert Anthropic message format to OpenAI format.
DisabledThinkingEffortResolver
¶
Resolve and cache the effort used for Anthropic disabled thinking.
_probe()
async
¶
Use low if the renderer rejects or ignores none.
none is ignored when it renders the same prompt as a thinking
effort, e.g. GLM-5.3 treats unknown efforts as max.
_CountTokensChatRequest
¶
Bases: ChatCompletionRequest
Chat request rendered by count_tokens, which counts prompts longer than max_model_len instead of rejecting them, like /tokenize.
_StreamError
¶
Bases: Exception
Carries the ErrorInfo from an upstream ErrorResponse through the stream
converter so the SSE error event can reuse its status code to pick the
Anthropic error type.
_build_anthropic_usage(usage)
¶
Build an AnthropicUsage from UsageInfo.
Anthropic defines total_input == input_tokens + cache_read +
cache_creation. vLLM's prompt_tokens is the total, so
input_tokens = prompt_tokens - cache_read - cache_creation.
Cache fields are taken from UsageInfo.prompt_tokens_details.
When cache info is absent (e.g. --enable-prompt-tokens-details
off, or a streaming chunk that hasn't carried it yet), cache fields
are left unset so exclude_unset=True serialization omits them
entirely.
completion_tokens follows UsageInfo and may be None on
intermediate stream chunks; we coerce to 0 for the wire format.
_to_anthropic_stop_reason(finish_reason, stop_reason, tool_used, tool_complete)
¶
Map an OpenAI finish reason to Anthropic (stop_reason, stop_sequence).
Anthropic reports tool_use whenever the model invoked a tool. The chat
layer reports finish_reason="stop" for a named (forced) tool call, so
tool_used records whether a tool_use block was produced.
max_tokens always wins. A matched stop string wins unless the tool
calls are complete (tool_complete): a call it cut short must not run.