Skip to content
Python

Fix KeyError 'none' crash in openai_compat_effort for unlisted model ids (#676)

Aashishac12118edba1
diff --git a/coworker/providers/effort.py b/coworker/providers/effort.py
index fb330e88..816b77b9 100644
--- a/coworker/providers/effort.py
+++ b/coworker/providers/effort.py
@@ -65,11 +65,18 @@ def validate_level(value: Any, source: str = "reasoning_effort") -> Optional[str
def nearest_supported(level: str, supported: Sequence[str]) -> Optional[str]:
- """The accepted level closest in rank to `level`; ties resolve to the higher one."""
+ """The accepted level closest in rank to `level`; ties resolve to the higher one.
+
+ Levels outside `_RANK` (e.g. "none", an effort value OpenAI-compatible endpoints
+ accept but which is not a ranking level) return None — the caller omits the
+ parameter instead of crashing, since absence of the parameter is valid for
+ OpenAI-compatible endpoints."""
if not supported:
return None
if level in supported:
return level
+ if level not in _RANK:
+ return None
target = _RANK[level]
return min(supported, key=lambda s: (abs(_RANK[s] - target), -_RANK[s]))
@@ -155,7 +162,8 @@ _OPENAI_DEFAULT = ("low", "medium", "high")
def openai_compat_effort(model: str, level: str) -> EffortPlan:
supported = _OPENAI_COMPAT.get(model) or _OPENAI_COMPAT.get(model.lower()) or _OPENAI_DEFAULT
effective = nearest_supported(level, supported)
- assert effective is not None
+ if effective is None:
+ return unsupported(level, f"effort level {level!r} unknown for {model}; no effort parameter sent")
verified = model in _OPENAI_COMPAT or model.lower() in _OPENAI_COMPAT
note = ""
if effective != level:
diff --git a/tests/test_reasoning_effort.py b/tests/test_reasoning_effort.py
index fecc6506..102108d8 100644
--- a/tests/test_reasoning_effort.py
+++ b/tests/test_reasoning_effort.py
@@ -173,6 +173,27 @@ def test_openai_compat_mapping(model, level, effective, noted):
assert plan.params == {"reasoning_effort": effective}
assert bool(plan.note) == noted
+def test_nearest_supported_unknown_level_returns_none():
+ """Levels outside _RANK (e.g. "none") must not raise — see #676."""
+ assert nearest_supported("none", ("low", "medium", "high")) is None
+
+
+def test_openai_compat_unknown_level_sends_no_parameter():
+ """Effort "none" (used by the compaction summariser) and unlisted model ids
+ (Ollama, any OpenAI-compatible endpoint) must omit the parameter rather
+ than crash the turn — see #676."""
+ plan = openai_compat_effort("ollama:glm-5.3:cloud", "none")
+ assert plan.effective is None
+ assert plan.params == {}
+ assert "unknown" in plan.note
+
+ # Known models behave identically — "none" omits the parameter there too
+ plan = openai_compat_effort("moonshotai/Kimi-K3", "none")
+ assert plan.effective is None
+ assert plan.params == {}
+
+
+
def test_every_curated_model_resolves_every_level():
"""No level on any curated row may raise; providers without a knob are simply skipped."""