reasoning_timeouts.py 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Per-model stale-timeout FLOOR for known reasoning models.
  2. Reasoning models routinely exceed the default chat-model stale detectors (stream 180s,
  3. non-stream 90s): upstream proxies idle-kill the stream mid-think, surfacing as
  4. ``BrokenPipeError``/``RemoteProtocolError``. The stale-detector scaling applies
  5. ``max(default, floor)`` from :func:`get_reasoning_stale_timeout_floor`, so this never
  6. overrides explicit per-model ``stale_timeout_seconds``/``request_timeout_seconds`` (that
  7. branch never calls it), never lowers a threshold, and is ``None`` for non-allowlisted models.
  8. """
  9. from __future__ import annotations
  10. import re
  11. from typing import Optional
  12. # floor_seconds -> slugs. Order irrelevant — longest slug wins at match time.
  13. _REASONING_STALE_TIMEOUT_FLOORS: dict[int, tuple[str, ...]] = {
  14. 600: (
  15. # NVIDIA Nemotron behind hosted NIM: documented 60-180s upstream idle kill.
  16. "nemotron-3-ultra", "nemotron-3-super", "nemotron-3.5-super-vl",
  17. # DeepSeek R1 / V4 (reasoning_content streamed before final content).
  18. # ``deepseek-flash`` is the version-less canonical Flash id (2026-09 Flash refresh);
  19. # ``deepseek-v4-flash`` still aliases onto it server-side.
  20. "deepseek-r1", "deepseek-reasoner", "deepseek-flash", "deepseek-v4-flash", "deepseek-v4.1-flash", "deepseek-v4-pro",
  21. # OpenAI o-series: each variant enumerated so bare ``o1`` cannot over-match ``olmo-1``.
  22. "o1", "o1-mini", "o1-pro", "o1-preview", "o3", "o3-pro",
  23. # OpenAI named reasoning lines (gpt-5.6-sol/-terra/-luna, gpt-6-astra, their -pro/-900k
  24. # variants): minutes-long thinking at xhigh/max/ultra; sub-10k-token requests sit below the
  25. # Codex context-size floor, so this is their only protection. Anchored so gpt-5.5 and the
  26. # gpt-4.x / gpt-5.1-chat lines keep the effort-tier defaults (#112909).
  27. "gpt-5.6", "gpt-6",
  28. # Mythos-class named models (claude-fable-5): 1M ctx + 128K output, a heavier thinking
  29. # phase than the numbered line — otherwise the stale detector trips the circuit breaker.
  30. "claude-fable",
  31. # Gemini 4 (gemini-4-argon): thinks longer than 3.x, and a long-decode turn re-prefills the
  32. # whole output so far at every hop before the next token arrives.
  33. "gemini-4",
  34. ),
  35. 300: (
  36. "nemotron-3-nano", "nemotron-3.5-lightning", "qwq-32b", "o3-mini", "o4-mini",
  37. # xAI Grok: explicit reasoning pairs only, so bare ``grok-3``/``grok-4`` fast variants
  38. # don't inherit the floor.
  39. "grok-4-fast-reasoning", "grok-4.20-reasoning", "grok-4.5", "grok-4.6",
  40. # "Ox Alpha" stealth reasoning model (OpenRouter / OpenCode Zen slugs); Thinking
  41. # Machines Inkling (covers inkling-small and :free SKUs).
  42. "ox-alpha", "x-preview-f-free", "inkling",
  43. # MiniMax M2.x (m2.5/m2.7): reasoning_content before first content token; 240s
  44. # mid-think stalls observed (#62353).
  45. "minimax-m2",
  46. ),
  47. # Anthropic Claude 4.x+ thinking variants (anchored so 3.x never matches).
  48. 240: ("claude-opus-4", "claude-opus-5"),
  49. # qwen3 family: instruct variants also match — a slightly longer wait on a hung provider
  50. # beats a pattern (``qwen3-.*-thinking``) that breaks on the next naming shape.
  51. 180: ("qwen3", "claude-sonnet-5", "claude-sonnet-4.5", "claude-sonnet-4.6", "grok-4-fast-non-reasoning"),
  52. }
  53. # Pre-compiled once at import (immutable afterwards — safe under free-threaded Python).
  54. # Right anchor: end-of-string or a slug separator; ``:`` because OpenRouter routing suffixes
  55. # (``:free``, ``:nitro``) attach directly to the slug. Longest-first so ``o3-mini`` beats ``o3``.
  56. _SORTED_REASONING_FLOORS: list[tuple[str, float, re.Pattern[str]]] = [
  57. (slug, floor, re.compile(r"^" + re.escape(slug) + r"(?:$|[\-._:])"))
  58. for slug, floor in sorted(
  59. ((slug, floor) for floor, slugs in _REASONING_STALE_TIMEOUT_FLOORS.items() for slug in slugs),
  60. key=lambda kv: -len(kv[0]),
  61. )
  62. ]
  63. def get_reasoning_stale_timeout_floor(model: object) -> Optional[float]:
  64. """Stale-timeout floor (seconds) for a known reasoning model, else ``None``.
  65. The aggregator prefix (up to the last ``/``) is stripped and the slug matched
  66. start-anchored with an end-or-separator right anchor, so ``qwen3-235b`` matches ``qwen3``
  67. but ``some-other-qwen3`` and ``llama-4-70b-o1-preview`` do not.
  68. """
  69. if not model or not isinstance(model, str):
  70. return None
  71. name = model.strip().lower().rsplit("/", 1)[-1]
  72. for _slug, floor, pattern in _SORTED_REASONING_FLOORS:
  73. if pattern.search(name):
  74. return float(floor)
  75. return None