style(anthropic-messages): apply ruff format to handler.py

This commit is contained in:
Daniel Cherubini 2026-07-07 15:41:40 +02:00
parent b83939c22e
commit db87a72850

View file

@ -117,9 +117,7 @@ async def _prepare_context_managed_request(
messages=messages,
system=system,
)
working_messages = (
history_result.messages if history_result is not None else messages
)
working_messages = history_result.messages if history_result is not None else messages
working_system = history_result.system if history_result is not None else system
polyfill_result: Final = await _run_polyfill_if_enabled(
@ -176,10 +174,7 @@ def _polyfill_will_run(
COMPACT_EDIT_TYPE,
)
return any(
isinstance(edit, dict) and edit.get("type") == COMPACT_EDIT_TYPE
for edit in edits
)
return any(isinstance(edit, dict) and edit.get("type") == COMPACT_EDIT_TYPE for edit in edits)
def _spec_has_non_compact_edits(
@ -206,9 +201,7 @@ def _spec_has_non_compact_edits(
)
return any(
isinstance(edit, dict)
and isinstance(edit.get("type"), str)
and edit.get("type") != COMPACT_EDIT_TYPE
isinstance(edit, dict) and isinstance(edit.get("type"), str) and edit.get("type") != COMPACT_EDIT_TYPE
for edit in edits
)
@ -296,9 +289,7 @@ async def _run_polyfill_if_enabled(
# 400. Other exception types fall into the best-effort branch below.
raise
except Exception as e:
verbose_logger.exception(
"context_management polyfill: skipping edits due to error: %s", e
)
verbose_logger.exception("context_management polyfill: skipping edits due to error: %s", e)
# Best-effort swallow is only safe for compact-only specs, where the
# caller's compaction-block-slicing safety net produces a correct
# (if degraded) result. When the spec also requested non-compact
@ -327,9 +318,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
@staticmethod
def _is_thinking_disabled(thinking: Optional[Dict]) -> bool:
"""Return True when the client's thinking param is absent or explicitly disabled."""
return thinking is None or (
isinstance(thinking, dict) and thinking.get("type") == "disabled"
)
return thinking is None or (isinstance(thinking, dict) and thinking.get("type") == "disabled")
@staticmethod
def _route_openai_thinking_to_responses_api_if_needed(
@ -366,9 +355,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
model: Final = completion_kwargs.get("model")
try:
model_info = get_model_info(
model=cast(str, model), custom_llm_provider=custom_llm_provider
)
model_info = get_model_info(model=cast(str, model), custom_llm_provider=custom_llm_provider)
if model_info and model_info.get("supports_reasoning") is False:
# Model doesn't support reasoning/responses API, don't route
return
@ -391,13 +378,8 @@ class LiteLLMMessagesToCompletionTransformationHandler:
reasoning_dict["summary"] = "detailed"
completion_kwargs["reasoning_effort"] = reasoning_dict
elif isinstance(reasoning_effort, dict):
if (
"summary" not in reasoning_effort
and "generate_summary" not in reasoning_effort
):
effective_summary = (
summary if summary else ("detailed" if auto_summary else None)
)
if "summary" not in reasoning_effort and "generate_summary" not in reasoning_effort:
effective_summary = summary if summary else ("detailed" if auto_summary else None)
if effective_summary:
updated_reasoning_effort: Final = dict(reasoning_effort)
updated_reasoning_effort["summary"] = effective_summary
@ -432,9 +414,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
completion_kwargs["reasoning_effort"] = normalized
elif isinstance(reasoning_effort, dict) and "effort" in reasoning_effort:
effort = reasoning_effort["effort"]
normalized = normalize_reasoning_effort_value(
effort, model=model, custom_llm_provider=custom_llm_provider
)
normalized = normalize_reasoning_effort_value(effort, model=model, custom_llm_provider=custom_llm_provider)
if normalized != effort:
completion_kwargs["reasoning_effort"] = {
**reasoning_effort,
@ -511,9 +491,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
(
openai_request,
tool_name_mapping,
) = ANTHROPIC_ADAPTER.translate_completion_input_params_with_tool_mapping(
request_data
)
) = ANTHROPIC_ADAPTER.translate_completion_input_params_with_tool_mapping(request_data)
if openai_request is None:
raise ValueError("Failed to translate request to OpenAI format")
@ -544,31 +522,19 @@ class LiteLLMMessagesToCompletionTransformationHandler:
# NOTE: extra_kwargs was already coerced from None to {} at the top of
# this method (line ~220). It is guaranteed to be a dict here.
for key, value in extra_kwargs.items():
if (
key == "litellm_logging_obj"
and value is not None
and isinstance(value, LiteLLMLoggingObject)
):
if key == "litellm_logging_obj" and value is not None and isinstance(value, LiteLLMLoggingObject):
from litellm.types.utils import CallTypes
setattr(value, "call_type", CallTypes.anthropic_messages.value)
setattr(
value, "stream_options", completion_kwargs.get("stream_options")
)
if (
key not in excluded_keys
and key not in completion_kwargs
and value is not None
):
setattr(value, "stream_options", completion_kwargs.get("stream_options"))
if key not in excluded_keys and key not in completion_kwargs and value is not None:
completion_kwargs[key] = value
# Normalize reasoning_effort based on model capabilities
# (e.g. "max" → "xhigh"/"high", "minimal" → "low" if unsupported)
# Must run BEFORE _route_openai_thinking, which prepends "responses/"
# to the model name and would break get_model_info() lookups.
LiteLLMMessagesToCompletionTransformationHandler._normalize_reasoning_effort(
completion_kwargs
)
LiteLLMMessagesToCompletionTransformationHandler._normalize_reasoning_effort(completion_kwargs)
LiteLLMMessagesToCompletionTransformationHandler._route_openai_thinking_to_responses_api_if_needed(
completion_kwargs,
@ -597,9 +563,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
) -> AnthropicMessagesResponse | AsyncIterator[bytes] | Iterator[bytes]:
"""Handle non-Anthropic models asynchronously using the adapter"""
context_management = kwargs.pop("context_management", None)
additional_drop_params: Optional[list[str]] = kwargs.get(
"additional_drop_params", None
)
additional_drop_params: Optional[list[str]] = kwargs.get("additional_drop_params", None)
litellm_router = kwargs.pop("litellm_router", None)
if litellm_router is None:
try:
@ -623,12 +587,8 @@ class LiteLLMMessagesToCompletionTransformationHandler:
user_api_key_auth=user_api_key_auth,
)
effective_messages = (
polyfill_result.messages if polyfill_result is not None else messages
)
effective_system = (
polyfill_result.system if polyfill_result is not None else system
)
effective_messages = polyfill_result.messages if polyfill_result is not None else messages
effective_system = polyfill_result.system if polyfill_result is not None else system
(
completion_kwargs,
@ -653,22 +613,16 @@ class LiteLLMMessagesToCompletionTransformationHandler:
completion_response: Final = await litellm.acompletion(**completion_kwargs)
thinking_disabled = (
LiteLLMMessagesToCompletionTransformationHandler._is_thinking_disabled(
thinking
)
)
thinking_disabled = LiteLLMMessagesToCompletionTransformationHandler._is_thinking_disabled(thinking)
if stream:
transformed_stream = (
ANTHROPIC_ADAPTER.translate_completion_output_params_streaming(
completion_response,
model=model,
tool_name_mapping=tool_name_mapping,
polyfill_result=polyfill_result,
is_async=True,
thinking_disabled=thinking_disabled,
)
transformed_stream = ANTHROPIC_ADAPTER.translate_completion_output_params_streaming(
completion_response,
model=model,
tool_name_mapping=tool_name_mapping,
polyfill_result=polyfill_result,
is_async=True,
thinking_disabled=thinking_disabled,
)
if transformed_stream is not None:
return transformed_stream
@ -734,9 +688,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
# ``compact_20260112`` editor can ``await`` the summarization model);
# bridge to it via ``run_async_function``.
context_management = kwargs.pop("context_management", None)
additional_drop_params: Optional[list[str]] = kwargs.get(
"additional_drop_params", None
)
additional_drop_params: Optional[list[str]] = kwargs.get("additional_drop_params", None)
# Deliberately do NOT auto-attach the proxy ``llm_router`` here:
# ``run_async_function`` spawns a new event loop in a worker thread
# to bridge to the async dispatcher, but the proxy router's httpx
@ -773,12 +725,8 @@ class LiteLLMMessagesToCompletionTransformationHandler:
user_api_key_auth=user_api_key_auth,
)
effective_messages = (
polyfill_result.messages if polyfill_result is not None else messages
)
effective_system = (
polyfill_result.system if polyfill_result is not None else system
)
effective_messages = polyfill_result.messages if polyfill_result is not None else messages
effective_system = polyfill_result.system if polyfill_result is not None else system
(
completion_kwargs,
@ -803,22 +751,16 @@ class LiteLLMMessagesToCompletionTransformationHandler:
completion_response: Final = litellm.completion(**completion_kwargs)
thinking_disabled = (
LiteLLMMessagesToCompletionTransformationHandler._is_thinking_disabled(
thinking
)
)
thinking_disabled = LiteLLMMessagesToCompletionTransformationHandler._is_thinking_disabled(thinking)
if stream:
transformed_stream = (
ANTHROPIC_ADAPTER.translate_completion_output_params_streaming(
completion_response,
model=model,
tool_name_mapping=tool_name_mapping,
polyfill_result=polyfill_result,
is_async=False,
thinking_disabled=thinking_disabled,
)
transformed_stream = ANTHROPIC_ADAPTER.translate_completion_output_params_streaming(
completion_response,
model=model,
tool_name_mapping=tool_name_mapping,
polyfill_result=polyfill_result,
is_async=False,
thinking_disabled=thinking_disabled,
)
if transformed_stream is not None:
return transformed_stream