Add fallback mechanism for OpenAIModel completion requests

This commit is contained in:
Mimikko-zeus
2026-03-03 01:42:51 +08:00
parent 0b356d1fef
commit 0def2deb9b
2 changed files with 310 additions and 2 deletions

View File

@@ -91,6 +91,36 @@ class OpenAIModel(BaseAIModel):
),
}
async def _create_completion_with_fallback(
self, params: Dict[str, Any], tools: Optional[List[dict]]
):
"""Create completion with runtime fallback for old SDK signatures."""
try:
return await self.client.chat.completions.create(**params)
except TypeError as exc:
message = str(exc)
if "unexpected keyword argument 'tools'" in message and "tools" in params:
self.logger.warning(
"SDK rejected `tools` at runtime, retrying with legacy functions."
)
self._supports_tools = False
retry_params = dict(params)
retry_params.pop("tools", None)
retry_params.update(self._build_tool_params(tools))
return await self.client.chat.completions.create(**retry_params)
if "unexpected keyword argument 'functions'" in message and "functions" in params:
self.logger.warning(
"SDK rejected `functions` at runtime, retrying without tool calling."
)
self._supports_functions = False
retry_params = dict(params)
retry_params.pop("functions", None)
retry_params.pop("function_call", None)
return await self.client.chat.completions.create(**retry_params)
raise
async def chat(
self,
messages: List[Message],
@@ -113,7 +143,7 @@ class OpenAIModel(BaseAIModel):
params.update(self._build_tool_params(tools))
params.update(kwargs)
response = await self.client.chat.completions.create(**params)
response = await self._create_completion_with_fallback(params, tools)
choice = response.choices[0]
tool_calls = self._extract_response_tool_calls(choice.message)
@@ -143,7 +173,7 @@ class OpenAIModel(BaseAIModel):
params.update(self._build_tool_params(tools))
params.update(kwargs)
stream = await self.client.chat.completions.create(**params)
stream = await self._create_completion_with_fallback(params, tools)
async for chunk in stream:
delta = chunk.choices[0].delta