Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(tencent): satisfy basedpyright budget in thinking mapping

Felipe Rodrigues Gare Carnielli1065856548cc
diff --git a/litellm/llms/tencent/chat/transformation.py b/litellm/llms/tencent/chat/transformation.py
index 283a227943d..7e80b0012df 100644
--- a/litellm/llms/tencent/chat/transformation.py
+++ b/litellm/llms/tencent/chat/transformation.py
@@ -27,8 +27,8 @@ class ThinkingPayload(TypedDict, total=False):
budget_tokens: ReadOnly[int]
-class TencentExtraBody(TypedDict, total=False):
- """`extra_body` payload for TokenHub chat requests."""
+class ThinkingExtraBody(TypedDict, total=False):
+ """`extra_body` payload carrying TokenHub's `thinking` object."""
thinking: ReadOnly[Mapping[str, object]]
@@ -54,21 +54,17 @@ class TencentChatConfig(OpenAIGPTConfig):
thinking: Final = self._resolve_thinking_payload(
model=model,
- thinking_value=thinking_value,
- reasoning_effort=reasoning_effort,
+ thinking_value=thinking_value, # pyright: ignore[reportUnknownArgumentType] # value popped from the untyped provider params dict
+ reasoning_effort=reasoning_effort, # pyright: ignore[reportUnknownArgumentType] # value popped from the untyped provider params dict
)
- if thinking is None:
- return mapped_params
-
- # TokenHub expects `thinking` in the request JSON body, but the OpenAI
- # SDK's chat.completions.create() rejects unknown top-level kwargs, so
- # it travels via `extra_body`, which the SDK merges into the payload.
- existing_extra_body: Final = mapped_params.pop("extra_body", None)
- if isinstance(existing_extra_body, dict):
- merged_extra_body: Final[TencentExtraBody] = {**existing_extra_body, "thinking": thinking}
- else:
- merged_extra_body: Final[TencentExtraBody] = {"thinking": thinking}
- mapped_params["extra_body"] = merged_extra_body
+ if thinking is not None:
+ # TokenHub expects `thinking` in the request JSON body, but the
+ # OpenAI SDK's chat.completions.create() rejects unknown top-level
+ # kwargs, so it travels via `extra_body`, which the SDK merges into
+ # the payload. A plain assignment is merge-safe: get_optional_params
+ # spreads this dict into its own extra_body assembly downstream.
+ extra_body: Final[ThinkingExtraBody] = {"thinking": thinking}
+ mapped_params["extra_body"] = extra_body
return mapped_params
@classmethod
@@ -79,7 +75,7 @@ class TencentChatConfig(OpenAIGPTConfig):
reasoning_effort: object,
) -> Mapping[str, object] | None:
if isinstance(thinking_value, dict):
- return cls._coerce_thinking_type_for_model(model=model, thinking=thinking_value)
+ return cls._coerce_thinking_type_for_model(model=model, thinking=thinking_value) # pyright: ignore[reportUnknownArgumentType] # isinstance narrows to dict[Unknown, Unknown] out of the untyped provider params dict
if isinstance(reasoning_effort, str):
# TokenHub recommends explicitly disabling thinking rather than
# relying on per-model defaults (deepseek-v4-* default to enabled).
@@ -101,7 +97,7 @@ class TencentChatConfig(OpenAIGPTConfig):
if thinking.get("type") != "enabled" or not TencentChatConfig._is_adaptive_thinking_model(model):
return thinking
- budget: Final = thinking.get("budget_tokens")
+ budget: Final[object] = thinking.get("budget_tokens")
if isinstance(budget, int):
coerced_with_budget: Final[ThinkingPayload] = {"type": "adaptive", "budget_tokens": budget}
return coerced_with_budget
@@ -112,7 +108,7 @@ class TencentChatConfig(OpenAIGPTConfig):
def _is_adaptive_thinking_model(model: str) -> bool:
"""Read `supports_adaptive_thinking` from the model map under tencent."""
try:
- model_info: Final = litellm.get_model_info(model=model, custom_llm_provider="tencent")
+ model_info: Final[Mapping[str, object]] = litellm.get_model_info(model=model, custom_llm_provider="tencent")
except Exception: # noqa: BLE001 # get_model_info raises a bare Exception for unmapped models
return False
return model_info.get("supports_adaptive_thinking") is True
diff --git a/tests/test_litellm/llms/tencent/chat/test_tencent_chat_transformation.py b/tests/test_litellm/llms/tencent/chat/test_tencent_chat_transformation.py
index a540ea6cacd..e8f5db09c4b 100644
--- a/tests/test_litellm/llms/tencent/chat/test_tencent_chat_transformation.py
+++ b/tests/test_litellm/llms/tencent/chat/test_tencent_chat_transformation.py
@@ -118,7 +118,9 @@ def test_map_openai_params_extracts_thinking_and_effort_from_optional_params():
assert "reasoning_effort" not in result
-def test_map_openai_params_merges_into_existing_extra_body():
+def test_map_openai_params_overwrites_existing_extra_body():
+ """The map layer assigns extra_body directly; get_optional_params merges it
+ with user-supplied extra params downstream (utils.py provider overrides)."""
config = TencentChatConfig()
result = config.map_openai_params(
non_default_params={},
@@ -130,7 +132,28 @@ def test_map_openai_params_merges_into_existing_extra_body():
drop_params=False,
)
- assert result["extra_body"] == {"custom_flag": True, "thinking": {"type": "enabled"}}
+ assert result["extra_body"] == {"thinking": {"type": "enabled"}}
+
+
+def test_get_optional_params_merges_thinking_with_user_extra_body():
+ """End-to-end at the get_optional_params layer: a user-supplied extra_body
+ and the mapped thinking payload must coexist in the final extra_body."""
+ from litellm.utils import get_optional_params
+
+ with patch(
+ "litellm.llms.tencent.chat.transformation.supports_reasoning",
+ return_value=True,
+ ):
+ result = get_optional_params(
+ model="tencent/deepseek-v4-pro",
+ custom_llm_provider="tencent",
+ messages=[{"role": "user", "content": "hi"}],
+ thinking={"type": "enabled"},
+ extra_body={"custom_flag": True},
+ )
+
+ assert result["extra_body"]["thinking"] == {"type": "enabled"}
+ assert result["extra_body"]["custom_flag"] is True
def test_transform_request_never_passes_thinking_as_top_level_kwarg():