From f654e226af6acf873ef82a7f33947e4ccc5c3f7e Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 18 May 2026 12:45:02 -0500 Subject: [PATCH] fix: guard LLM response access before dereferencing choices/content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit response.choices[0].message.content is Optional[str] in the OpenAI SDK — returns None (not raises) when finish_reason is tool_calls or the provider filters output. message.content[0].text is similarly unsafe on the Anthropic side when content is empty or filtered. Adds guards in three files: - autoagent/environment/mdconvert.py (OpenAI) - autoagent/environment/markdown_browser/mdconvert.py (OpenAI) - docs/translation_updater.py (Anthropic) --- autoagent/environment/markdown_browser/mdconvert.py | 2 ++ autoagent/environment/mdconvert.py | 2 ++ docs/translation_updater.py | 2 ++ 3 files changed, 6 insertions(+) diff --git a/autoagent/environment/markdown_browser/mdconvert.py b/autoagent/environment/markdown_browser/mdconvert.py index 196c452..7c3e667 100644 --- a/autoagent/environment/markdown_browser/mdconvert.py +++ b/autoagent/environment/markdown_browser/mdconvert.py @@ -789,6 +789,8 @@ def _get_mlm_description(self, local_path, extension, client, model, prompt=None ] response = client.chat.completions.create(model=model, messages=messages) + if not response.choices or response.choices[0].message is None or response.choices[0].message.content is None: + raise ValueError("LLM returned empty or filtered response") return response.choices[0].message.content diff --git a/autoagent/environment/mdconvert.py b/autoagent/environment/mdconvert.py index 56c14e8..b377088 100644 --- a/autoagent/environment/mdconvert.py +++ b/autoagent/environment/mdconvert.py @@ -796,6 +796,8 @@ def _get_mlm_description(self, local_path, extension, client, model, prompt=None ] response = client.chat.completions.create(model=model, messages=messages) + if not response.choices or response.choices[0].message is None or response.choices[0].message.content is None: + raise ValueError("LLM returned empty or filtered response") return response.choices[0].message.content diff --git a/docs/translation_updater.py b/docs/translation_updater.py index f30c6c5..7eaa946 100644 --- a/docs/translation_updater.py +++ b/docs/translation_updater.py @@ -61,6 +61,8 @@ def translate_content(content, target_lang): ], ) + if not message.content or message.content[0].text is None: + raise ValueError("LLM returned empty or filtered response") return message.content[0].text