Skip to content

Commit ca26779

Browse files
committed
feat: limit images to 20 for AWS Bedrock browser tool usage
- Add TOO_MANY_IMAGES error type to Bedrock error handling - Create image limiting utility that keeps most recent 20 images - Replace older images with descriptive text placeholders - Integrate image limiting into Bedrock message conversion - Add comprehensive tests for image limiting functionality Fixes #6348
1 parent 75f93c4 commit ca26779

File tree

3 files changed

+522
-2
lines changed

3 files changed

+522
-2
lines changed

src/api/providers/bedrock.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { MultiPointStrategy } from "../transform/cache-strategy/multi-point-stra
3030
import { ModelInfo as CacheModelInfo } from "../transform/cache-strategy/types"
3131
import { convertToBedrockConverseMessages as sharedConverter } from "../transform/bedrock-converse-format"
3232
import { getModelParams } from "../transform/model-params"
33+
import { applyBedrockImageLimiting } from "../transform/image-limiting"
3334
import { shouldUseReasoningBudget } from "../../shared/api"
3435
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
3536

@@ -706,8 +707,11 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
706707
modelInfo?: any,
707708
conversationId?: string, // Optional conversation ID to track cache points across messages
708709
): { system: SystemContentBlock[]; messages: Message[] } {
710+
// Apply image limiting before conversion to prevent "too many images" errors
711+
const limitedMessages = applyBedrockImageLimiting(anthropicMessages as Anthropic.Messages.MessageParam[])
712+
709713
// First convert messages using shared converter for proper image handling
710-
const convertedMessages = sharedConverter(anthropicMessages as Anthropic.Messages.MessageParam[])
714+
const convertedMessages = sharedConverter(limitedMessages)
711715

712716
// If prompt caching is disabled, return the converted messages directly
713717
if (!usePromptCache) {
@@ -737,7 +741,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
737741
const config = {
738742
modelInfo: cacheModelInfo,
739743
systemPrompt: systemMessage,
740-
messages: anthropicMessages as Anthropic.Messages.MessageParam[],
744+
messages: limitedMessages,
741745
usePromptCache,
742746
previousCachePointPlacements: previousPlacements,
743747
}
@@ -1178,6 +1182,15 @@ Please try:
11781182
messageTemplate: `Invalid ARN format. ARN should follow the pattern: arn:aws:bedrock:region:account-id:resource-type/resource-name`,
11791183
logLevel: "error",
11801184
},
1185+
TOO_MANY_IMAGES: {
1186+
patterns: ["too many images", "too many images and documents"],
1187+
messageTemplate: `Too many images in conversation. AWS Bedrock has a limit of 20 images per conversation.
1188+
1189+
The system has automatically limited the conversation to the 20 most recent images to resolve this issue. Older images have been replaced with descriptive text placeholders.
1190+
1191+
This is normal behavior when using the browser tool extensively, as each browser action captures a screenshot.`,
1192+
logLevel: "info",
1193+
},
11811194
VALIDATION_ERROR: {
11821195
patterns: [
11831196
"input tag",
@@ -1235,6 +1248,7 @@ Please check:
12351248
"SERVICE_QUOTA_EXCEEDED", // Most specific - check before THROTTLING
12361249
"MODEL_NOT_READY",
12371250
"TOO_MANY_TOKENS",
1251+
"TOO_MANY_IMAGES", // Check for image limit errors
12381252
"INTERNAL_SERVER_ERROR",
12391253
"ON_DEMAND_NOT_SUPPORTED",
12401254
"NOT_FOUND",

0 commit comments

Comments
 (0)