Skip to content

fix: graceful error handling for empty/invalid Fara model responses#484

Open
majiayu000 wants to merge 2 commits intomicrosoft:mainfrom
majiayu000:fix/fara-empty-response-handling
Open

fix: graceful error handling for empty/invalid Fara model responses#484
majiayu000 wants to merge 2 commits intomicrosoft:mainfrom
majiayu000:fix/fara-empty-response-handling

Conversation

@majiayu000
Copy link

Summary

Test plan

  • Added 10 unit tests for _parse_thoughts_and_action method
  • All existing tests pass
  • poe fmt - formatting passes
  • poe lint - linting passes
  • poe pyright - type checking passes

Fixes microsoft#453

- Add validation for empty model responses with helpful error message
- Add check for missing <tool_call> tag with debugging info
- Handle case where <tool_call> is not followed by newline
- Add check for empty tool call content
- Improve error messages to help users diagnose model compatibility issues
- Add comprehensive unit tests for _parse_thoughts_and_action method

Signed-off-by: majiayu000 <[email protected]>
levidehaan pushed a commit to levidehaan/magentic-ui that referenced this pull request Dec 31, 2025
- Add Anthropic Claude as a model provider option in the UI
  - New AnthropicModelConfigForm component for configuring Claude models
  - Support for claude-sonnet-4, claude-3-5-sonnet, claude-3-5-haiku, claude-3-opus
  - Backend validation for Anthropic provider aliases
  - Updated autogen-ext dependency to include anthropic extra

- Improve Fara web surfer error handling (PR microsoft#484)
  - Add validation for empty/whitespace-only model responses
  - Check for required <tool_call> tag before parsing
  - Handle tool calls with or without newline delimiters
  - Provide descriptive error messages with response previews
  - Fall back gracefully from JSON to ast.literal_eval parsing
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes issue #453 where the Fara integration crashes with an IndexError when using LM Studio instead of vLLM, caused by empty or malformed model responses. The fix adds comprehensive validation and error handling to gracefully handle these cases with helpful error messages for users.

Changes:

  • Added validation for empty/whitespace-only model responses with descriptive error messages
  • Added check for missing <tool_call> tag with response preview for debugging
  • Made the parser more flexible to handle <tool_call> tags not followed by newlines
  • Added validation for empty tool call content
  • Improved exception handling and error messages with response truncation
  • Added 10 comprehensive unit tests covering all validation branches and edge cases

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/test_fara_web_surfer.py New test file with 10 unit tests covering all validation logic, edge cases, and error handling in the parsing method
src/magentic_ui/agents/web_surfer/fara/_fara_web_surfer.py Enhanced _parse_thoughts_and_action method with validation checks, improved parsing flexibility, and better error messages

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 200 to 243
try:
action = json.loads(action_text)
except json.decoder.JSONDecodeError:
self.logger.error(f"Invalid action text: {action_text}")
self.logger.warning(
f"JSON decode failed, trying ast.literal_eval: {action_text}"
)
action = ast.literal_eval(action_text)

return thoughts, action
except ValueError:
raise
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception handling here could mask errors from ast.literal_eval. When ast.literal_eval raises a ValueError (e.g., due to malformed syntax), it will be re-raised at line 243 without the additional context added at lines 248-251. Consider catching specific exceptions from ast.literal_eval or restructuring to ensure all parsing failures include the helpful error message with response preview.

Copilot uses AI. Check for mistakes.
Comment on lines 216 to 222
if len(tmp) < 2:
response_preview = (
f"{message[:200]}..." if len(message) > 200 else message
)
raise ValueError(
f"Could not parse tool call from response: {response_preview}"
)
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition at line 216 is unreachable. Since we already verified that "<tool_call>" exists in the message at line 203, splitting by "<tool_call>" at line 215 will always produce at least 2 parts. This check serves no practical purpose and could be removed to reduce code complexity.

Suggested change
if len(tmp) < 2:
response_preview = (
f"{message[:200]}..." if len(message) > 200 else message
)
raise ValueError(
f"Could not parse tool call from response: {response_preview}"
)

Copilot uses AI. Check for mistakes.
@majiayu000
Copy link
Author

Tested: ============================= test session starts ==============================
platform darwin -- Python 3.12.12, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/lifcc/Desktop/code/OpenSource/magentic-ui
configfile: pytest.ini
plugins: asyncio-1.1.0, anyio-4.10.0, cov-6.2.1
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function
collected 11 items

tests/test_fara_web_surfer.py ........... [100%]

============================== 11 passed in 2.81s ============================== (11 passed)

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.

Fara integration crashes when using LM Studio instead of vLLM (empty response triggers IndexError in _parse_thoughts_and_action)

1 participant