Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 5, 2025

Problem

The Anthropic Code Suggester plugin was incorrectly handling the response from Anthropic's Claude API. The API returns message.content as a list of content blocks (specifically [TextBlock(text="...", type="text")]), but the code was treating it as a simple string. This caused the plugin to return a list object instead of the actual text content, leading to JSON serialization issues and incorrect data display in the UI.

Before

return {
    "suggestions": message.content,  # Returns [TextBlock(...)] instead of text
    "model": "claude-3-5-sonnet-20241022",
    "status": "success",
}

When this executes, consumers would receive:

{
  "suggestions": [{"text": "Here are suggestions...", "type": "text"}],
  "status": "success"
}

Instead of the expected:

{
  "suggestions": "Here are suggestions...",
  "status": "success"
}

Solution

Added proper content extraction logic that:

  1. Validates the response structure (checks if content is a list with at least one element)
  2. Extracts the .text attribute from the first content block
  3. Handles edge cases with clear error messages (empty content, missing content, malformed responses)
  4. Follows defensive programming practices

After

# Extract text from content blocks (Anthropic API returns a list)
if message.content and isinstance(message.content, list) and len(message.content) > 0:
    # Get the text from the first content block
    suggestions_text = message.content[0].text if hasattr(message.content[0], 'text') else str(message.content[0])
else:
    logger.error("Anthropic API response missing expected content.")
    return {
        "error": "Anthropic API response missing expected content.",
        "status": "error"
    }

return {
    "suggestions": suggestions_text,  # Returns actual text string
    "model": "claude-3-5-sonnet-20241022",
    "status": "success",
}

Test Updates

Updated test mocks to accurately reflect the Anthropic API structure:

# Old mock (incorrect)
mock_message.content = "Some suggestions"

# New mock (correct)
mock_content_block = MagicMock()
mock_content_block.text = "Some suggestions"
mock_message.content = [mock_content_block]

Additionally:

  • Removed pytest dependency in favor of pure unittest
  • Added new test case for empty content validation
  • Fixed import paths to properly use the plugins module
  • Changed pytest.raises to self.assertRaises for consistency

Testing

All test cases now cover:

  • ✅ Normal response with text block
  • ✅ Empty content list
  • ✅ None content
  • ✅ Multiple content blocks (uses first)
  • ✅ API errors
  • ✅ Invalid inputs

Impact

  • ✅ Fixes JSON response handling bug
  • ✅ Improves error handling and validation
  • ✅ Better test coverage
  • ✅ Removes external test dependency (pytest → unittest)
  • ✅ No breaking changes to API
  • ✅ Maintains backward compatibility

Files Changed

  • plugins/anthropic_code_suggester.py - Fixed response extraction logic
  • tests/test_anthropic_code_suggester.py - Updated test mocks and added validation test
Original prompt

json fix 2


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: canstralian <8595080+canstralian@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] json fix 2 Fix Anthropic API response handling - extract text from content blocks Oct 5, 2025
@Copilot Copilot AI requested a review from canstralian October 5, 2025 01:28
Copilot finished work on behalf of canstralian October 5, 2025 01:28
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.

2 participants