Files
devplacepy/devplacepy/services/openai_gateway/thinking.py
T
retoorandClaude Sonnet 5.5 151476125f Gateway: auto-resolve request shape per provider and model
An upstream that rejects a key the gateway adds (e.g. thinking) now gets
retried with other shapes on a parsable 400/422; the working method is
stored per (provider, target model) and applied first-try from then on.
Fallback attempts are re-shaped per attempt. Admin list/reset, Devii
tools, audit event and ledger resolved_method included.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-13 11:34:26 +01:00

177 lines
5.5 KiB
Python

# retoor <retoor@molodetz.nl>
from dataclasses import dataclass
from typing import Any, Optional
CLIENT_THINKING_KEYS = (
"think",
"thinking",
"reasoning",
"reasoning_effort",
"enable_thinking",
)
FALSE_VALUES = {False, 0, "0", "false", "no", "off", "none", "disabled"}
TRUE_VALUES = {True, 1, "1", "true", "yes", "on", "enabled"}
EFFORT_VALUES = {"low", "medium", "high", "max", "xhigh"}
def thinking_dialect(url: str) -> str:
text = (url or "").lower()
if "deepseek.com" in text:
return "deepseek"
if "openrouter.ai" in text:
return "openrouter"
if "11434" in text or "ollama" in text:
return "ollama"
return "deepseek"
def _resolve_dialect(url: str, dialect: str) -> str:
effective = dialect.strip().lower()
if effective in ("", "auto"):
return thinking_dialect(url)
return effective
@dataclass(frozen=True)
class UpstreamCapabilities:
dialect: str
supports_stream_options: bool
supports_stream_usage: bool
supports_thinking_field: bool
def upstream_capabilities(
url: str, dialect: str = "", ollama_stream_usage: bool = False
) -> UpstreamCapabilities:
# Current Ollama docs list stream_options.include_usage as supported, but
# real deployed server versions vary and some reject it outright (see
# github.com/ollama/ollama issues #15288/#15293/#14820) - default off,
# gateway_ollama_stream_usage lets an operator opt in for a known-good build.
effective = _resolve_dialect(url, dialect)
if effective == "ollama":
return UpstreamCapabilities(
dialect=effective,
supports_stream_options=bool(ollama_stream_usage),
supports_stream_usage=bool(ollama_stream_usage),
supports_thinking_field=True,
)
return UpstreamCapabilities(
dialect=effective,
supports_stream_options=True,
supports_stream_usage=True,
supports_thinking_field=True,
)
def _as_text(value: Any) -> str:
if isinstance(value, str):
return value.strip().lower()
return ""
def _is_false(value: Any) -> bool:
if isinstance(value, str):
return value.strip().lower() in FALSE_VALUES
return value in FALSE_VALUES
def _is_true(value: Any) -> bool:
if isinstance(value, str):
text = value.strip().lower()
return text in TRUE_VALUES or text in EFFORT_VALUES
return value in TRUE_VALUES
def client_thinking_enabled(body: Any) -> Optional[bool]:
if not isinstance(body, dict):
return None
thinking = body.get("thinking")
if isinstance(thinking, dict) and "type" in thinking:
kind = _as_text(thinking.get("type"))
if kind in ("disabled", "none", "off"):
return False
if kind in ("enabled", "on"):
return True
if "think" in body:
value = body.get("think")
if _is_false(value):
return False
if _is_true(value):
return True
return bool(value)
reasoning = body.get("reasoning")
if isinstance(reasoning, dict):
if "enabled" in reasoning:
return _flag_enabled(reasoning.get("enabled"))
effort = _as_text(reasoning.get("effort"))
if effort == "none":
return False
if effort in EFFORT_VALUES:
return True
if "reasoning_effort" in body:
effort = _as_text(body.get("reasoning_effort"))
if effort == "none":
return False
if effort in EFFORT_VALUES:
return True
if "enable_thinking" in body:
return _flag_enabled(body.get("enable_thinking"))
kwargs = body.get("chat_template_kwargs")
if isinstance(kwargs, dict) and "enable_thinking" in kwargs:
return _flag_enabled(kwargs.get("enable_thinking"))
return None
def _flag_enabled(value: Any) -> bool:
if _is_false(value):
return False
if _is_true(value):
return True
return bool(value)
def _clear_thinking_fields(payload: dict) -> None:
for key in CLIENT_THINKING_KEYS:
payload.pop(key, None)
kwargs = payload.get("chat_template_kwargs")
if isinstance(kwargs, dict):
kwargs.pop("enable_thinking", None)
if not kwargs:
payload.pop("chat_template_kwargs", None)
def write_thinking(payload: dict, dialect: str, enabled: bool) -> None:
_clear_thinking_fields(payload)
if dialect == "none":
return
if dialect == "openrouter":
payload["reasoning"] = {"effort": "high" if enabled else "none"}
return
if dialect == "ollama":
# Ollama's native /api/chat honors a boolean `think`, but its
# OpenAI-compatible /v1/chat/completions layer does not (confirmed
# against github.com/ollama/ollama issues #15288/#15293/#14820,
# Sep 2026): that endpoint maps `reasoning_effort`/`reasoning.effort`
# onto its internal think flag instead. Send both - `think` is
# harmless if ignored, `reasoning_effort` is the field the
# OpenAI-compat endpoint actually honors.
payload["think"] = bool(enabled)
payload["reasoning_effort"] = "high" if enabled else "none"
return
payload["thinking"] = {"type": "enabled" if enabled else "disabled"}
def apply_thinking(
payload: dict,
upstream_url: str,
default_enabled: bool = False,
dialect: str = "",
) -> dict:
effective = _resolve_dialect(upstream_url, dialect)
specified = client_thinking_enabled(payload)
enabled = default_enabled if specified is None else specified
write_thinking(payload, effective, enabled)
return payload