mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-06 06:24:16 +00:00
Fix greptile comments
This commit is contained in:
@@ -464,6 +464,49 @@ async def aresponses(
|
||||
# Update local_vars with detected provider (fixes #19782)
|
||||
local_vars["custom_llm_provider"] = custom_llm_provider
|
||||
|
||||
#########################################################
|
||||
# ASYNC PROMPT MANAGEMENT
|
||||
#########################################################
|
||||
litellm_logging_obj = kwargs.get("litellm_logging_obj", None)
|
||||
prompt_id = cast(Optional[str], kwargs.get("prompt_id", None))
|
||||
prompt_variables = cast(Optional[dict], kwargs.get("prompt_variables", None))
|
||||
|
||||
if isinstance(litellm_logging_obj, LiteLLMLoggingObj) and litellm_logging_obj.should_run_prompt_management_hooks(
|
||||
prompt_id=prompt_id, non_default_params=kwargs
|
||||
):
|
||||
if isinstance(input, str):
|
||||
client_input: List[AllMessageValues] = [
|
||||
{"role": "user", "content": input}
|
||||
]
|
||||
else:
|
||||
client_input = [
|
||||
item # type: ignore[misc]
|
||||
for item in input
|
||||
if isinstance(item, dict) and "role" in item
|
||||
]
|
||||
(
|
||||
model,
|
||||
merged_input,
|
||||
merged_optional_params,
|
||||
) = await litellm_logging_obj.async_get_chat_completion_prompt(
|
||||
model=model,
|
||||
messages=client_input,
|
||||
non_default_params=kwargs,
|
||||
prompt_id=prompt_id,
|
||||
prompt_variables=prompt_variables,
|
||||
prompt_label=kwargs.get("prompt_label", None),
|
||||
prompt_version=kwargs.get("prompt_version", None),
|
||||
)
|
||||
input = cast(Union[str, ResponseInputParam], merged_input)
|
||||
if "/" in model:
|
||||
_, custom_llm_provider, _, _ = litellm.get_llm_provider(
|
||||
model=model
|
||||
)
|
||||
local_vars["custom_llm_provider"] = custom_llm_provider
|
||||
for k, v in merged_optional_params.items():
|
||||
if k in local_vars:
|
||||
local_vars[k] = v
|
||||
|
||||
func = partial(
|
||||
responses,
|
||||
input=input,
|
||||
@@ -633,11 +676,16 @@ def responses(
|
||||
if isinstance(litellm_logging_obj, LiteLLMLoggingObj) and litellm_logging_obj.should_run_prompt_management_hooks(
|
||||
prompt_id=prompt_id, non_default_params=kwargs
|
||||
):
|
||||
client_input: List[AllMessageValues] = (
|
||||
[{"role": "user", "content": input}]
|
||||
if isinstance(input, str)
|
||||
else cast(List[AllMessageValues], list(input))
|
||||
)
|
||||
if isinstance(input, str):
|
||||
client_input: List[AllMessageValues] = [
|
||||
{"role": "user", "content": input}
|
||||
]
|
||||
else:
|
||||
client_input = [
|
||||
item # type: ignore[misc]
|
||||
for item in input
|
||||
if isinstance(item, dict) and "role" in item
|
||||
]
|
||||
(
|
||||
model,
|
||||
merged_input,
|
||||
@@ -654,6 +702,11 @@ def responses(
|
||||
input = cast(Union[str, ResponseInputParam], merged_input)
|
||||
local_vars["input"] = input
|
||||
local_vars["model"] = model
|
||||
if "/" in model:
|
||||
_, custom_llm_provider, _, _ = litellm.get_llm_provider(
|
||||
model=model
|
||||
)
|
||||
local_vars["custom_llm_provider"] = custom_llm_provider
|
||||
for k, v in merged_optional_params.items():
|
||||
local_vars[k] = v
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ class TestResponsesAPIPromptManagement:
|
||||
]
|
||||
# Simulate get_chat_completion_prompt returning merged optional params
|
||||
# that include a template-defined temperature
|
||||
merged_kwargs = {"temperature": 0.2, "prompt_id": "t", "litellm_logging_obj": None}
|
||||
merged_kwargs = {"temperature": 0.2}
|
||||
|
||||
logging_obj = MagicMock()
|
||||
logging_obj.__class__ = LiteLLMLoggingObj
|
||||
@@ -209,3 +209,68 @@ class TestResponsesAPIPromptManagement:
|
||||
# The model passed to the downstream handler should be the overridden one
|
||||
handler_call_kwargs = mock_handler.call_args.kwargs
|
||||
assert handler_call_kwargs.get("model") == "openai/gpt-4o-mini"
|
||||
|
||||
def test_non_message_input_items_filtered(self):
|
||||
"""[F] Non-message items in ResponseInputParam (e.g. function_call_output) are
|
||||
filtered out before being passed to the prompt hook, avoiding malformed merges."""
|
||||
template_messages: List[AllMessageValues] = [
|
||||
{"role": "system", "content": "You are helpful."}, # type: ignore[list-item]
|
||||
]
|
||||
mixed_input = [
|
||||
{"role": "user", "content": "Hello"},
|
||||
{"type": "function_call_output", "call_id": "abc", "output": "42"},
|
||||
]
|
||||
logging_obj = _make_logging_obj(
|
||||
merged_model="openai/gpt-4o",
|
||||
merged_messages=template_messages + [{"role": "user", "content": "Hello"}], # type: ignore[operator]
|
||||
)
|
||||
|
||||
patches = _patch_responses_dispatch()
|
||||
with patches[0], patches[1], patches[2], patches[3]:
|
||||
import litellm
|
||||
litellm.responses(
|
||||
input=mixed_input, # type: ignore[arg-type]
|
||||
model="gpt-4o",
|
||||
prompt_id="filter-test",
|
||||
litellm_logging_obj=logging_obj,
|
||||
)
|
||||
|
||||
call_kwargs = logging_obj.get_chat_completion_prompt.call_args.kwargs
|
||||
passed_messages = call_kwargs["messages"]
|
||||
assert all(isinstance(m, dict) and "role" in m for m in passed_messages)
|
||||
assert len(passed_messages) == 1
|
||||
|
||||
def test_model_override_re_resolves_provider(self):
|
||||
"""[G] When the prompt template overrides the model to a different provider,
|
||||
custom_llm_provider is re-resolved so downstream routing uses the correct provider."""
|
||||
template_messages: List[AllMessageValues] = [
|
||||
{"role": "user", "content": "Hi"}, # type: ignore[list-item]
|
||||
]
|
||||
logging_obj = _make_logging_obj(
|
||||
merged_model="anthropic/claude-3-5-sonnet",
|
||||
merged_messages=template_messages,
|
||||
)
|
||||
|
||||
patches = _patch_responses_dispatch()
|
||||
with (
|
||||
patch(
|
||||
"litellm.responses.main.litellm.get_llm_provider",
|
||||
side_effect=[
|
||||
("gpt-4o", "openai", None, None),
|
||||
("claude-3-5-sonnet", "anthropic", None, None),
|
||||
],
|
||||
),
|
||||
patches[1],
|
||||
patches[2],
|
||||
patches[3] as mock_handler,
|
||||
):
|
||||
import litellm
|
||||
litellm.responses(
|
||||
input="Hi",
|
||||
model="gpt-4o",
|
||||
prompt_id="cross-provider",
|
||||
litellm_logging_obj=logging_obj,
|
||||
)
|
||||
|
||||
handler_call_kwargs = mock_handler.call_args.kwargs
|
||||
assert handler_call_kwargs.get("custom_llm_provider") == "anthropic"
|
||||
|
||||
Reference in New Issue
Block a user