@@ -262,26 +262,41 @@ async def prepare_provider_request(
262262
263263 # Parse body (format conversion is now handled by format chain)
264264 body_data = json .loads (body .decode ()) if body else {}
265- body_data = self ._apply_request_template (body_data )
265+ if self ._should_apply_detection_payload ():
266+ body_data = self ._apply_request_template (body_data )
267+ else :
268+ body_data = self ._normalize_input_messages (body_data )
266269
267- # Fetch detected instructions from detection service
268- instructions = self ._get_instructions ()
270+ detected_instructions = (
271+ self ._get_instructions () if self ._should_apply_detection_payload () else ""
272+ )
269273
270274 existing_instructions = body_data .get ("instructions" )
271275 if isinstance (existing_instructions , str ) and existing_instructions :
272- if instructions :
273- instructions = instructions + "\n " + existing_instructions
274- else :
275- instructions = existing_instructions
276+ instructions = (
277+ detected_instructions + "\n " + existing_instructions
278+ if detected_instructions
279+ else existing_instructions
280+ )
281+ else :
282+ instructions = detected_instructions
276283
277- body_data ["instructions" ] = instructions
284+ if instructions :
285+ body_data ["instructions" ] = instructions
286+ else :
287+ body_data .pop ("instructions" , None )
278288
279289 # Codex backend requires stream=true, always override
280290 body_data ["stream" ] = True
281291 body_data ["store" ] = False
282292
283293 # Remove unsupported keys for Codex
284- for key in ("max_output_tokens" , "max_completion_tokens" , "temperature" ):
294+ for key in (
295+ "max_output_tokens" ,
296+ "max_completion_tokens" ,
297+ "max_tokens" ,
298+ "temperature" ,
299+ ):
285300 body_data .pop (key , None )
286301
287302 list_input = body_data .get ("input" , [])
@@ -640,6 +655,9 @@ def _request_body_is_encoded(self, headers: dict[str, str]) -> bool:
640655 encoding = headers .get ("content-encoding" , "" ).strip ().lower ()
641656 return bool (encoding and encoding != "identity" )
642657
658+ def _should_apply_detection_payload (self ) -> bool :
659+ return bool (getattr (self .config , "inject_detection_payload" , True ))
660+
643661 def _detect_streaming_intent (self , body : bytes , headers : dict [str , str ]) -> bool :
644662 if self ._request_body_is_encoded (headers ):
645663 accept = headers .get ("accept" , "" ).lower ()
0 commit comments