Skip to content

fix: guard against None message in structured_output_response - #690

Open
qizwiz wants to merge 1 commit into
lastmile-ai:mainfrom
qizwiz:fix/llm-response-unguarded
Open

fix: guard against None message in structured_output_response#690
qizwiz wants to merge 1 commit into
lastmile-ai:mainfrom
qizwiz:fix/llm-response-unguarded

Conversation

@qizwiz

@qizwiz qizwiz commented May 18, 2026

Copy link
Copy Markdown

What

Fix two places in augmented_llm_openai.py where the existing guard

if not completion.choices or completion.choices[0].message.content is None:

is itself vulnerable to AttributeError when message is None.

Why

If completion.choices[0].message is None (e.g. Gemini via OpenAI-compatible endpoint on PROHIBITED_CONTENT, which returns HTTP 200 with message: null), accessing .content in the condition itself throws AttributeError: 'NoneType' object has no attribute 'content' — before the guard can fire.

Fix

# Before (crashes when message is None):
if not completion.choices or completion.choices[0].message.content is None:
    raise ValueError("No structured content returned by model")

# After (safe):
if not completion.choices or completion.choices[0].message is None:
    raise ValueError("No structured content returned by model")
content = completion.choices[0].message.content
if content is None:
    raise ValueError("No structured content returned by model")

Both the sync and async structured_output_response paths are fixed.
No behaviour change for well-formed responses.

Summary by CodeRabbit

  • Bug Fixes
    • Strengthened validation of AI model responses to more reliably detect incomplete or missing data
    • Enhanced error messages for structured output generation failures, providing clearer feedback when the model cannot generate the expected output

Review Change Stack

…ed_output_response

The existing checks test choices[0].message.content is None, but if
message itself is None (returned by Gemini and some other providers on
content-filtered responses with HTTP 200), the attribute access crashes
with AttributeError before the guard can fire. Fix both sync and async
paths to check message is None separately.
@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 570904be-df8a-4544-b4d0-75a306d38ab2

📥 Commits

Reviewing files that changed from the base of the PR and between f62d849 and d6ba7d0.

📒 Files selected for processing (1)
  • src/mcp_agent/workflows/llm/augmented_llm_openai.py

📝 Walkthrough

Walkthrough

The PR tightens structured-output validation in OpenAI response handlers by splitting combined null checks into granular steps. Two methods—generate_structured and request_structured_completion_task—now explicitly validate completion.choices existence, first choice message presence, and message.content non-nullity separately, raising ValueError with consistent messaging when any check fails.

Changes

Validation Tightening for Structured OpenAI Responses

Layer / File(s) Summary
Granular validation in OpenAI response handlers
src/mcp_agent/workflows/llm/augmented_llm_openai.py
Both generate_structured and request_structured_completion_task now validate completion.choices and first choice message separately before reading message.content, raising ValueError with consistent messaging when any step fails.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • saqadri
  • rholinshead

Poem

🐰 Oh, what validation precision fine,
Each choice and message we divine,
Split checks that guard the model's reply,
With structured care, no stone left dry! 🌿

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding guards against None message in structured output response handling.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant