Skip to content

fix(pi): guard empty base64 images, error tool_results, and sanitize tool IDs#17

Open
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:fix/pi-convert-guards
Open

fix(pi): guard empty base64 images, error tool_results, and sanitize tool IDs#17
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:fix/pi-convert-guards

Conversation

@iceteaSA
Copy link
Copy Markdown

Summary

Fixes three related bugs in the PI message converter that cause Anthropic API 400 errors:

  1. Tool ID sanitization — Tool-call IDs from non-Anthropic providers may contain characters outside ^[a-zA-Z0-9_-]+$. Added sanitizeToolId() that strips invalid chars, guards empty IDs with 'tool_call_unknown', and truncates at 256 chars. Applied at all 3 tool_use_id assignment sites.

  2. Empty base64 image guard — Image content blocks with empty/falsy data field are now filtered out instead of sending invalid base64 payloads to Anthropic.

  3. Empty error content guard — When is_error=true but the content array is empty, Anthropic rejects the request. Now injects { type: 'text', text: 'Error' } as fallback content.

Testing

All existing tests pass. These are edge cases triggered by cross-provider message conversion (e.g. OpenAI-format tool calls routed through PI).

…tool IDs

- Add sanitizeToolId() to strip chars outside [a-zA-Z0-9_-] from
  tool-call IDs, preventing Anthropic 400 errors from cross-provider
  ID incompatibility
- Guard empty base64 image data to prevent invalid payloads
- Inject fallback content for is_error=true tool_results with empty
  content, which Anthropic rejects
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