@@ -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
287292def _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 ,
0 commit comments