Passes through extra_ properties on "custom" llm provider (#12185)

* Passes through headers on "custom" llm provider

* add test

* adds extra_body support for custom llm providers
This commit is contained in:
Zayd 2025-07-01 18:07:38 -07:00 committed by GitHub
parent b3b4c65ac4
commit 7ef590df84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 0 deletions

View File

@ -3221,6 +3221,7 @@ def completion( # type: ignore # noqa: PLR0915
prompt = " ".join([message["content"] for message in messages]) # type: ignore
resp = litellm.module_level_client.post(
url,
headers=headers,
json={
"model": model,
"params": {
@ -3230,6 +3231,7 @@ def completion( # type: ignore # noqa: PLR0915
"top_p": top_p,
"top_k": kwargs.get("top_k"),
},
**kwargs.get("extra_body", {}),
},
)
response_json = resp.json()

View File

@ -268,6 +268,66 @@ def test_bedrock_latency_optimized_inference():
assert json_data["performanceConfig"]["latency"] == "optimized"
def test_custom_provider_with_extra_headers():
from litellm.llms.custom_httpx.http_handler import HTTPHandler
with patch.object(litellm.llms.custom_httpx.http_handler.HTTPHandler, "post") as mock_post:
response = litellm.completion(
model="custom/custom",
messages=[{"role": "user", "content": "Hello, how are you?"}],
headers={"X-Custom-Header": "custom-value"},
api_base="https://example.com/api/v1",
)
mock_post.assert_called_once()
assert mock_post.call_args[1]["headers"]["X-Custom-Header"] == "custom-value"
def test_custom_provider_with_extra_body():
from litellm.llms.custom_httpx.http_handler import HTTPHandler
with patch.object(litellm.llms.custom_httpx.http_handler.HTTPHandler, "post") as mock_post:
response = litellm.completion(
model="custom/custom",
messages=[{"role": "user", "content": "Hello, how are you?"}],
extra_body={"X-Custom-BodyValue": "custom-value", "X-Custom-BodyValue2": "custom-value2"},
api_base="https://example.com/api/v1",
)
mock_post.assert_called_once()
assert mock_post.call_args[1]["json"]["X-Custom-BodyValue"] == "custom-value"
assert mock_post.call_args[1]["json"] == {
'model': 'custom',
'params': {
'prompt': ['Hello, how are you?'],
'max_tokens': None,
'temperature': None,
'top_p': None,
'top_k': None
},
'X-Custom-BodyValue': 'custom-value',
'X-Custom-BodyValue2': 'custom-value2'
}
# test that extra_body is not passed if not provided
with patch.object(litellm.llms.custom_httpx.http_handler.HTTPHandler, "post") as mock_post:
response = litellm.completion(
model="custom/custom",
messages=[{"role": "user", "content": "Hello, how are you?"}],
api_base="https://example.com/api/v1",
)
mock_post.assert_called_once()
assert mock_post.call_args[1]["json"] == {
'model': 'custom',
'params': {
'prompt': ['Hello, how are you?'],
'max_tokens': None,
'temperature': None,
'top_p': None,
'top_k': None
}
}
@pytest.fixture(autouse=True)
def set_openrouter_api_key():
original_api_key = os.environ.get("OPENROUTER_API_KEY")