fix: guard against empty choices in OpenAI and AstraFlow LLM providers

The OpenAI-compatible API can return HTTP 200 with an empty choices list
or choices[0].message = None (content-filtered responses on Gemini,
overwhelmed Ollama instances). Without a guard, both sites raise an
unhandled IndexError or AttributeError crashing the provider.

Added guard in OpenAIProvider.generate() and AstraFlowProvider.generate().
This commit is contained in:
Your Name
2026-05-17 23:49:00 -05:00
parent 43822b9c1a
commit cc62e89152
2 changed files with 5 additions and 1 deletions

View File

@@ -79,6 +79,8 @@ class _AstraflowBaseProvider(LLMProvider):
params["tools"] = [tool.to_openai_tool() for tool in llm_input.tools]
response = self.client.chat.completions.create(**params)
if not response.choices or response.choices[0].message is None:
raise ValueError("LLM returned empty or filtered response")
choice = response.choices[0]
tool_calls = None

View File

@@ -67,9 +67,11 @@ class OpenAIProvider(LLMProvider):
if input.max_tokens:
params["max_tokens"] = input.max_tokens
if input.tools:
params["tools"] = [tool.to_openai_tool() for tool in input.tools]
params["tools"] = [tool.to_openai_tool() for tool in input.tools]
response = self.client.chat.completions.create(**params)
if not response.choices or response.choices[0].message is None:
raise ValueError("LLM returned empty or filtered response")
choice = response.choices[0]
tool_calls = None