Skip to content

Conversation

kuzuha
Copy link
Contributor

@kuzuha kuzuha commented Jun 4, 2025

What this PR does

Fixes Issue #500: Incorrect nesting of cachePoint blocks inside ToolMessage causes Bedrock API request validation errors.

Before (Invalid Bedrock format)

The cachePoint block is nested under toolResult.content, which is not allowed by Bedrock API:

{
  "role": "user",
  "content": [
    {
      "toolResult": {
        "content": [
          {"text": "Tool response"},
          {"cachePoint": {"type": "default"}}  // ❌ Invalid here!
        ],
        "toolUseId": "xyz",
        "status": "success"
      }
    }
  ]
}

After (Valid Bedrock format)

This PR hoists the cachePoint block to be a top-level sibling entry within the message's content array:

{
  "role": "user",
  "content": [
    {
      "toolResult": {
        "content": [
          {"text": "Tool response"}
        ],
        "toolUseId": "xyz",
        "status": "success"
      }
    },
    {
      "cachePoint": {"type": "default"}  // ✅ Valid placement
    }
  ]
}

Implementation details

  • In _messages_to_bedrock, ToolMessage.content is split into:
    • tool_result_content: Blocks safe to nest under toolResult (e.g., text)
    • special_blocks: Blocks like cachePoint that must be top-level
  • Both are placed accordingly in the final Bedrock message structure.

Test coverage

Added test_messages_to_bedrock_with_cache_point() in test_bedrock_converse.py to validate:

  • Proper splitting of blocks
  • Correct nesting according to API expectations

Reference


Fixes #500

Copy link
Collaborator

@michaelnchin michaelnchin left a comment

Choose a reason for hiding this comment

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

Thank you @kuzuha !

@michaelnchin michaelnchin merged commit f7621d6 into langchain-ai:main Jun 6, 2025
12 checks passed
@kuzuha kuzuha deleted the fix/issue-500 branch June 6, 2025 03:12
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.

Incorrect nesting of cachePoint in ToolMessage causes Bedrock API error
2 participants