Skip to content

Commit ca7553a

Browse files
committed
fix(providers): the chatgpt provider resends an effort the served model refuses at its floor
- the plan serves whichever model it serves; one refuses `none` where the last accepted it, and the 400 lists the accepted levels, so the call resends at the lowest and the session keeps that floor
1 parent e4435bf commit ca7553a

2 files changed

Lines changed: 77 additions & 6 deletions

File tree

‎src/agent6/providers/chatgpt.py‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,15 @@ def parse_output_items(
278278
)
279279

280280

281-
def _no_adapt(status: int | None, text: str, body: dict[str, Any]) -> bool:
282-
"""Return False: this wire adapts no body."""
283-
del status, text, body
284-
return False
281+
_EFFORT_UNSUPPORTED = re.compile(r"Supported values are:\s*((?:'[a-z]+'(?:,\s*(?:and\s*)?)?)+)")
282+
283+
284+
def _accepted_efforts(status: int | None, text: str) -> tuple[str, ...]:
285+
"""Return the effort levels a 400 lists for `reasoning.effort`, in the order given."""
286+
if status != 400 or '"reasoning.effort"' not in text or "unsupported_value" not in text:
287+
return ()
288+
m = _EFFORT_UNSUPPORTED.search(text)
289+
return tuple(re.findall(r"'([a-z]+)'", m.group(1))) if m else ()
285290

286291

287292
def _unreachable_hook(data: dict[str, Any]) -> Any:
@@ -461,6 +466,8 @@ class ChatGPTProvider:
461466
session_id: str = dataclasses.field(default_factory=lambda: str(uuid.uuid4()))
462467
# One usage preflight per provider; a list, since the dataclass is frozen.
463468
_preflighted: list[bool] = dataclasses.field(default_factory=lambda: [False])
469+
# The effort levels the served model accepts, learned from its 400; empty until then.
470+
_efforts_accepted: list[tuple[str, ...]] = dataclasses.field(default_factory=lambda: [()])
464471

465472
def preflight(self) -> agent6_budget.PlanUsage | None:
466473
"""Read the account's plan state off the backend's `/usage` before any call.
@@ -485,6 +492,30 @@ def preflight(self) -> agent6_budget.PlanUsage | None:
485492
return None
486493
return plan_usage_from_usage_body(body) if isinstance(body, dict) else None
487494

495+
def _adapt_effort_400(self, status: int | None, text: str, body: dict[str, Any]) -> bool:
496+
"""Resend at the lowest effort the served model accepts, and keep that floor.
497+
498+
The plan serves whichever model it serves; one refuses `none`, the next may not. The
499+
400 names the accepted levels, lowest first.
500+
501+
Args:
502+
status: The HTTP status.
503+
text: The error text.
504+
body: The request body, rewritten in place.
505+
506+
Returns:
507+
Whether the body was adapted, so the transport retries once.
508+
"""
509+
accepted = _accepted_efforts(status, text)
510+
if not accepted:
511+
return False
512+
reasoning = body.get("reasoning")
513+
if not isinstance(reasoning, dict) or reasoning.get("effort") in accepted:
514+
return False
515+
self._efforts_accepted[0] = accepted
516+
reasoning["effort"] = accepted[0]
517+
return True
518+
488519
def _build_headers(self, token: str) -> dict[str, str]:
489520
"""Return one attempt's request headers, built from its token."""
490521
headers = {
@@ -569,6 +600,9 @@ def call(
569600
if effort:
570601
# Omitting the field leaves the model's default on; "off" needs the explicit "none".
571602
wire = "none" if effort == "off" else effort
603+
accepted = self._efforts_accepted[0]
604+
if accepted and wire not in accepted:
605+
wire = accepted[0] # the served model refused the level once; its floor stands in
572606
body["reasoning"] = {"effort": wire, "summary": "auto"}
573607
if self.extra_body:
574608
reserved = {
@@ -596,8 +630,8 @@ def call(
596630
budget=self.budget,
597631
model=self.model,
598632
build_headers=self._build_headers,
599-
adapt_400=_no_adapt,
600-
adapt_attempts=0,
633+
adapt_400=self._adapt_effort_400,
634+
adapt_attempts=1,
601635
# Stream-only, so the non-streaming hooks are unreachable; metering happens in-stream.
602636
require_metered=_unreachable_hook,
603637
parse=_unreachable_hook,

‎tests/unit/test_chatgpt_provider.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,3 +1183,40 @@ def test_two_message_items_in_a_response_stay_separated() -> None:
11831183
stop_reason="end_turn",
11841184
)
11851185
assert resp.text == "First message.\n\nSecond message."
1186+
1187+
1188+
def test_an_effort_the_served_model_refuses_is_resent_at_its_lowest_accepted_level(
1189+
signed_in: chatgpt_oauth.ChatGPTCredential,
1190+
) -> None:
1191+
"""A 400 naming `reasoning.effort` unsupported resends at the listed floor, then keeps it.
1192+
1193+
The plan serves whichever model it serves; one refuses `none` where the last accepted it.
1194+
"""
1195+
provider = _provider(signed_in, reasoning_effort="off")
1196+
refusal = json.dumps(
1197+
{
1198+
"error": {
1199+
"message": "Unsupported value: 'none' is not supported with the 'gpt-x' model."
1200+
" Supported values are: 'low', 'medium', 'high', 'xhigh', and 'max'.",
1201+
"type": "invalid_request_error",
1202+
"param": "reasoning.effort",
1203+
"code": "unsupported_value",
1204+
}
1205+
}
1206+
)
1207+
sent: list[str] = []
1208+
1209+
def fake_stream(method: str, url: str, **kwargs: Any) -> _FakeStreamResponse:
1210+
del method, url
1211+
body = json.loads(kwargs["content"])
1212+
sent.append(body["reasoning"]["effort"])
1213+
if len(sent) == 1:
1214+
return _FakeStreamResponse(status_code=400, lines=[], error_body=refusal)
1215+
return _FakeStreamResponse(status_code=200, lines=_happy_stream())
1216+
1217+
with mock.patch("httpx2.stream", side_effect=fake_stream):
1218+
first = provider.call(system="SYS", messages=[{"role": "user", "content": "t"}], tools=[])
1219+
second = provider.call(system="SYS", messages=[{"role": "user", "content": "t"}], tools=[])
1220+
1221+
assert first.text == "hello" and second.text == "hello"
1222+
assert sent == ["none", "low", "low"], "refused once, then the floor without another 400"

0 commit comments

Comments
 (0)