Repository navigation
feat(messages): passthrough /v1/messages to native endpoints via supported_endpoints #31685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mateo-berri
merged 7 commits into
litellm_internal_staging
from
litellm_lit3750_messages_passthrough
Jun 30, 2026
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab17460
feat(messages): passthrough /v1/messages to native endpoints via supp…
mateo-berri 926593e
fix(messages): preserve standard-cased caller headers in native passt…
mateo-berri b600631
fix(openai_like/messages): delegate to parent transform and inject an…
cursoragent 107b468
fix: normalize anthropic-beta header key case before beta injection
cursoragent 1f49ef3
style: collapse anthropic-beta header normalization to single line
mateo-berri fd2d145
fix(messages): forward anthropic-beta to native passthrough upstream
mateo-berri 4c1ce65
chore: remove accidentally committed local QA logs and config
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| from typing import Any, Optional | ||
|
|
||
| from litellm.llms.anthropic.experimental_pass_through.messages.transformation import ( | ||
| AnthropicMessagesConfig, | ||
| ) | ||
| from litellm.types.router import GenericLiteLLMParams | ||
|
|
||
| DEFAULT_ANTHROPIC_API_VERSION = "2023-06-01" | ||
|
|
||
|
|
||
| class OpenAILikeAnthropicMessagesConfig(AnthropicMessagesConfig): | ||
| """ | ||
| Forwards Anthropic /v1/messages requests to an OpenAI-compatible server that | ||
| also natively exposes the Anthropic Messages API, with no translation. | ||
|
|
||
| Opted into per deployment via ``model_info.supported_endpoints`` containing | ||
| ``"/v1/messages"``. The inbound Anthropic payload (system, cache_control, | ||
| thinking, tools, ...) is forwarded essentially unchanged to | ||
| ``{api_base}/v1/messages``, so Anthropic-only features that the | ||
| Anthropic->OpenAI translation would otherwise drop are preserved. Response | ||
| parsing and streaming are inherited from the native Anthropic config. | ||
| """ | ||
|
|
||
| def validate_anthropic_messages_environment( | ||
| self, | ||
| headers: dict[str, str], | ||
| model: str, | ||
| messages: list[Any], | ||
| optional_params: dict, | ||
| litellm_params: dict, | ||
| api_key: Optional[str] = None, | ||
| api_base: Optional[str] = None, | ||
| ) -> tuple[dict[str, str], Optional[str]]: | ||
| present = {key.lower() for key in headers} | ||
| needs_auth = bool(api_key) and "authorization" not in present and "x-api-key" not in present | ||
| defaults: dict[str, str] = { | ||
| **({"authorization": f"Bearer {api_key}"} if needs_auth else {}), | ||
| **({"anthropic-version": DEFAULT_ANTHROPIC_API_VERSION} if "anthropic-version" not in present else {}), | ||
| **({"content-type": "application/json"} if "content-type" not in present else {}), | ||
| } | ||
| return {**headers, **defaults}, api_base | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| def get_complete_url( | ||
| self, | ||
| api_base: Optional[str], | ||
| api_key: Optional[str], | ||
| model: str, | ||
| optional_params: dict, | ||
| litellm_params: dict, | ||
| stream: Optional[bool] = None, | ||
| ) -> str: | ||
| if not api_base: | ||
| raise ValueError("api_base is required to forward Anthropic /v1/messages to a native endpoint") | ||
| base = api_base.rstrip("/") | ||
| if base.endswith("/v1/messages"): | ||
| return base | ||
| if base.endswith("/v1"): | ||
| base = base[: -len("/v1")] | ||
| return f"{base}/v1/messages" | ||
|
|
||
| def transform_anthropic_messages_request( | ||
| self, | ||
| model: str, | ||
| messages: list[dict], | ||
| anthropic_messages_optional_request_params: dict, | ||
| litellm_params: GenericLiteLLMParams, | ||
| headers: dict, | ||
| ) -> dict: | ||
| if anthropic_messages_optional_request_params.get("max_tokens") is None: | ||
| raise ValueError("max_tokens is required for the Anthropic /v1/messages API") | ||
| return { | ||
| "model": model, | ||
| "messages": messages, | ||
| **anthropic_messages_optional_request_params, | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
173 changes: 173 additions & 0 deletions
173
...t_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import pytest | ||
|
|
||
| from litellm.llms.openai_like.messages.transformation import ( | ||
| OpenAILikeAnthropicMessagesConfig, | ||
| ) | ||
| from litellm.types.router import GenericLiteLLMParams | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def config() -> OpenAILikeAnthropicMessagesConfig: | ||
| return OpenAILikeAnthropicMessagesConfig() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "api_base, expected", | ||
| [ | ||
| ("https://host/v1", "https://host/v1/messages"), | ||
| ("https://host/v1/", "https://host/v1/messages"), | ||
| ("https://host", "https://host/v1/messages"), | ||
| ("https://host/v1/messages", "https://host/v1/messages"), | ||
| ("https://api.deepseek.com/anthropic", "https://api.deepseek.com/anthropic/v1/messages"), | ||
| ("https://api.deepseek.com/anthropic/v1", "https://api.deepseek.com/anthropic/v1/messages"), | ||
| ], | ||
| ) | ||
| def test_get_complete_url_handles_api_base_variants(config, api_base, expected): | ||
| url = config.get_complete_url( | ||
| api_base=api_base, | ||
| api_key="sk-test", | ||
| model="some-model", | ||
| optional_params={}, | ||
| litellm_params={}, | ||
| ) | ||
| assert url == expected | ||
|
|
||
|
|
||
| def test_get_complete_url_requires_api_base(config): | ||
| with pytest.raises(ValueError, match="api_base is required"): | ||
| config.get_complete_url( | ||
| api_base=None, | ||
| api_key="sk-test", | ||
| model="some-model", | ||
| optional_params={}, | ||
| litellm_params={}, | ||
| ) | ||
|
|
||
|
|
||
| def test_request_stays_in_anthropic_shape(config): | ||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "Summarize this", | ||
| "cache_control": {"type": "ephemeral"}, | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
| optional_params = { | ||
| "max_tokens": 256, | ||
| "system": "You are a careful assistant", | ||
| "thinking": {"type": "enabled", "budget_tokens": 1024}, | ||
| "temperature": 0.3, | ||
| "tools": [{"name": "lookup", "input_schema": {"type": "object"}}], | ||
| "stream": False, | ||
| } | ||
|
|
||
| payload = config.transform_anthropic_messages_request( | ||
| model="some-model", | ||
| messages=messages, | ||
| anthropic_messages_optional_request_params=optional_params, | ||
| litellm_params=GenericLiteLLMParams(), | ||
| headers={}, | ||
| ) | ||
|
|
||
| assert payload["model"] == "some-model" | ||
| assert payload["messages"] == messages | ||
| assert payload["messages"][0]["content"][0]["cache_control"] == {"type": "ephemeral"} | ||
| assert payload["system"] == "You are a careful assistant" | ||
| assert payload["thinking"] == {"type": "enabled", "budget_tokens": 1024} | ||
| assert payload["max_tokens"] == 256 | ||
| assert payload["tools"] == optional_params["tools"] | ||
|
|
||
| openai_only_keys = { | ||
| "max_completion_tokens", | ||
| "stop", | ||
| "n", | ||
| "logprobs", | ||
| "response_format", | ||
| "frequency_penalty", | ||
| } | ||
| assert openai_only_keys.isdisjoint(payload.keys()) | ||
|
|
||
|
|
||
| def test_request_requires_max_tokens(config): | ||
| with pytest.raises(ValueError, match="max_tokens is required"): | ||
| config.transform_anthropic_messages_request( | ||
| model="some-model", | ||
| messages=[{"role": "user", "content": "hi"}], | ||
| anthropic_messages_optional_request_params={"system": "s"}, | ||
| litellm_params=GenericLiteLLMParams(), | ||
| headers={}, | ||
| ) | ||
|
|
||
|
|
||
| def test_validate_environment_sets_bearer_and_anthropic_defaults(config): | ||
| headers, api_base = config.validate_anthropic_messages_environment( | ||
| headers={}, | ||
| model="some-model", | ||
| messages=[], | ||
| optional_params={}, | ||
| litellm_params={}, | ||
| api_key="sk-test", | ||
| api_base="https://host/v1", | ||
| ) | ||
| assert headers["authorization"] == "Bearer sk-test" | ||
| assert headers["anthropic-version"] == "2023-06-01" | ||
| assert headers["content-type"] == "application/json" | ||
| assert api_base == "https://host/v1" | ||
|
|
||
|
|
||
| def test_validate_environment_does_not_overwrite_caller_headers(config): | ||
| headers, _ = config.validate_anthropic_messages_environment( | ||
| headers={ | ||
| "authorization": "Bearer caller-token", | ||
| "anthropic-version": "2024-10-22", | ||
| "content-type": "application/json", | ||
| }, | ||
| model="some-model", | ||
| messages=[], | ||
| optional_params={}, | ||
| litellm_params={}, | ||
| api_key="sk-test", | ||
| api_base="https://host/v1", | ||
| ) | ||
| assert headers["authorization"] == "Bearer caller-token" | ||
| assert headers["anthropic-version"] == "2024-10-22" | ||
|
|
||
|
|
||
| def test_validate_environment_preserves_standard_cased_caller_headers(config): | ||
| headers, _ = config.validate_anthropic_messages_environment( | ||
| headers={ | ||
| "Authorization": "Bearer caller-token", | ||
| "Anthropic-Version": "2024-10-22", | ||
| "Content-Type": "application/json", | ||
| }, | ||
| model="some-model", | ||
| messages=[], | ||
| optional_params={}, | ||
| litellm_params={}, | ||
| api_key="sk-test", | ||
| api_base="https://host/v1", | ||
| ) | ||
| lowercased = {key.lower() for key in headers} | ||
| assert len(lowercased) == len(headers) | ||
| assert headers["Authorization"] == "Bearer caller-token" | ||
| assert headers["Anthropic-Version"] == "2024-10-22" | ||
| assert headers["Content-Type"] == "application/json" | ||
|
|
||
|
|
||
| def test_validate_environment_honors_x_api_key_when_present(config): | ||
| headers, _ = config.validate_anthropic_messages_environment( | ||
| headers={"X-Api-Key": "caller-key"}, | ||
| model="some-model", | ||
| messages=[], | ||
| optional_params={}, | ||
| litellm_params={}, | ||
| api_key="sk-test", | ||
| api_base="https://host/v1", | ||
| ) | ||
| assert "authorization" not in {key.lower() for key in headers} | ||
| assert headers["X-Api-Key"] == "caller-key" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.