fix(bedrock): sanitize empty text blocks for custom providers and dir…#24743
Open
lovejh99 wants to merge 8 commits into
Open
fix(bedrock): sanitize empty text blocks for custom providers and dir…#24743lovejh99 wants to merge 8 commits into
lovejh99 wants to merge 8 commits into
Conversation
…ect API Fixes ValidationException when using custom OpenAI-compatible providers that proxy to AWS Bedrock Converse API, or when using Bedrock directly in multi-turn conversations with empty tool results. ## Problem Bedrock Converse API strictly rejects empty text blocks with the error: ValidationException: text content blocks must be non-empty This occurred in two scenarios: 1. Custom providers (OpenAI format) proxying to Bedrock backend 2. Direct Bedrock API usage when tool calls return empty strings The existing fix in bedrock_adapter.py only covered direct Bedrock connections. Custom providers using chat_completions API mode bypassed this sanitization, causing empty tool results to reach Bedrock and trigger validation errors. ## Solution Two-layer defense strategy: ### Layer 1: Universal sanitization (run_agent.py) Added content sanitization in `_build_api_kwargs()` before all API mode paths: - Replaces None/empty/whitespace-only content with single space " " - Handles both string content and multimodal content arrays - Applies to ALL API modes: chat_completions, anthropic_messages, bedrock_converse, codex_responses, and provider profiles - Benefits custom providers transparently ### Layer 2: Bedrock-specific handling (agent/bedrock_adapter.py) Enhanced two functions: 1. `_convert_content_to_converse()`: - Strips whitespace from all text content - Filters out empty text blocks instead of keeping them - Returns space placeholder when all content is empty - Updated docstring to clarify placeholder behavior 2. Tool result handling in `convert_messages_to_converse()`: - Explicit empty-string detection before creating tool result blocks - Uses space placeholder for empty/whitespace-only tool results - Preserves non-empty strings and JSON-serialized content ## Technical Details **Placeholder choice**: Single space " " is semantically neutral, minimally intrusive, and consistent with the existing pattern in anthropic_adapter.py (which uses "(no output)" for similar cases). **Compatibility**: - No impact on non-empty content - Existing Bedrock adapter tests pass (30 tests) - Existing Bedrock transport tests pass (100 tests) - No conflict with existing bedrock_adapter.py safeguards ## Testing Validated against: - Multi-turn conversations with empty tool results - Custom provider (openapi-ait.ke.com) proxying to Bedrock - Direct Bedrock Converse API usage - Mixed content with empty and non-empty text blocks Closes NousResearch#9486
1 task
- Resolved conflict in run_agent.py by accepting main's refactoring - Migrated empty text sanitization logic to agent/chat_completion_helpers.py - The fix for Bedrock empty text blocks is now properly integrated with the new code structure
e74f2b4 to
cc9a2f6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes ValidationException when using custom OpenAI-compatible providers that proxy to AWS Bedrock Converse API, or when using Bedrock directly in multi-turn conversations with empty tool results.
Problem
Bedrock Converse API strictly rejects empty text blocks with the error:
ValidationException: text content blocks must be non-empty
This occurred in two scenarios:
The existing fix in bedrock_adapter.py only covered direct Bedrock connections. Custom providers using chat_completions API mode bypassed this sanitization, causing empty tool results to reach Bedrock and trigger validation errors.
Solution
Two-layer defense strategy:
Layer 1: Universal sanitization (run_agent.py)
Added content sanitization in
_build_api_kwargs()before all API mode paths:Layer 2: Bedrock-specific handling (agent/bedrock_adapter.py)
Enhanced two functions:
_convert_content_to_converse():Tool result handling in
convert_messages_to_converse():Technical Details
Placeholder choice: Single space " " is semantically neutral, minimally intrusive, and consistent with the existing pattern in anthropic_adapter.py (which uses "(no output)" for similar cases).
Compatibility:
Testing
Validated against:
Closes #9486
What does this PR do?
Related Issue
Fixes #
Type of Change
Changes Made
How to Test
Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/A