Skip to content

Conversation

@lanej
Copy link
Contributor

@lanej lanej commented Jul 25, 2025

Summary

This PR fixes 400 Bad Request errors that occur when Anthropic thinking blocks are present in messages. Without this fix, thinking blocks are either filtered out or ignored during translation, resulting in incomplete or malformed content being sent to the OpenAI API.

Changes

  • Add proper handling of thinking blocks in non-stream translation by merging them with text blocks
  • Update mapContent function to handle thinking blocks in both string and ContentPart formats
  • Include thinking blocks in the type imports from anthropic-types
  • Convert thinking content to text since GitHub Copilot/OpenAI doesn't support thinking blocks natively

Test plan

  • Add comprehensive tests for thinking blocks with and without tool calls
  • Verify thinking content is properly combined with text content
  • Confirm OpenAI API compatibility validation passes
  • Run existing tests with bun test

Fixes #61

🤖 Generated with Claude Code

- Add proper handling of thinking blocks in non-stream translation
- Filter and combine thinking blocks with text blocks for OpenAI compatibility
- Update mapContent function to handle thinking blocks in both string and ContentPart formats
- Add comprehensive tests for thinking blocks with and without tool calls
- Since GitHub Copilot doesn't support thinking blocks natively, merge thinking content with text

Fixes ericc-ch#61
@lanej lanej force-pushed the feat/thinking-blocks-support branch from 6ec3edd to 0d3c786 Compare July 25, 2025 20:16
@lanej lanej changed the title feat: add support for Anthropic thinking blocks fix: handle Anthropic thinking blocks to prevent 400 errors Jul 25, 2025
lanej and others added 4 commits July 25, 2025 13:32
The .loose() method doesn't exist in Zod - replace with .passthrough()
to allow additional properties in the anthropicStreamEventSchema.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Since thinking blocks are an Anthropic-specific feature, the tests should
use an Anthropic model instead of gpt-4o for accuracy and clarity.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ericc-ch
Copy link
Owner

Thanks!

@ericc-ch ericc-ch merged commit 6a40e43 into ericc-ch:master Jul 26, 2025
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.

Status Code: 400 - happened while web searching

2 participants