{"record":{"id":"4378af081a483bb6","repo":"FlowiseAI/Flowise","slug":"an-invalid-response-was-returned-by-bedrock","errorCode":null,"errorMessage":"An invalid response was returned by Bedrock.","messagePattern":"An invalid response was returned by Bedrock\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/nodes/embeddings/AWSBedrockEmbedding/AWSBedrockEmbedding.ts","lineNumber":214,"sourceCode":"const embedTextTitan = async (text: string, client: BedrockRuntimeClient, model: string): Promise<number[]> => {\n    const cleanedText = text.replace(/\\n/g, ' ')\n\n    const res = await client.send(\n        new InvokeModelCommand({\n            modelId: model,\n            body: JSON.stringify({\n                inputText: cleanedText\n            }),\n            contentType: 'application/json',\n            accept: 'application/json'\n        })\n    )\n\n    try {\n        const body = new TextDecoder().decode(res.body)\n        return JSON.parse(body).embedding\n    } catch (e) {\n        throw new Error('An invalid response was returned by Bedrock.')\n    }\n}\n\nconst embedTextCohere = async (texts: string[], client: BedrockRuntimeClient, model: string, inputType: string): Promise<number[][]> => {\n    const cleanedTexts = texts.map((text) => text.replace(/\\n/g, ' '))\n\n    const command = {\n        modelId: model,\n        body: JSON.stringify({\n            texts: cleanedTexts,\n            input_type: inputType,\n            truncate: 'END'\n        }),\n        contentType: 'application/json',\n        accept: 'application/json'\n    }\n    const res = await client.send(new InvokeModelCommand(command))\n    try {","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/nodes/embeddings/AWSBedrockEmbedding/AWSBedrockEmbedding.ts#L196-L232","documentation":"embedTextTitan catches any failure while decoding the Bedrock InvokeModel response body or reading the `.embedding` field. If JSON.parse throws (non-JSON body) or `.embedding` is undefined/ non-numeric, this generic message fires — masking whether the issue is transport, model, or response shape.","triggerScenarios":"Bedrock returns 200 with an unexpected body for a Titan embed call — e.g. wrong modelId returning an error object, truncated response, IAM allowing invoke but model not enabled in region, or a model that returns a different JSON key.","commonSituations":"modelId typo (e.g. a generation model instead of an embed model), model not enabled in the configured region, response throttled into a non-JSON error, or a Bedrock SDK version changing the body encoding.","solutions":["Confirm modelId is an embedding model (amazon.titan-embed-* ) and is enabled in the region.","Log res.body and the parsed JSON before reading .embedding to see the actual shape.","Verify IAM permissions include bedrock:InvokeModel for the model.","Pin the AWS SDK version; check for region/model availability changes.","Handle non-JSON bodies explicitly instead of relying on the catch-all."],"exampleFix":"// before\ntry {\n    const body = new TextDecoder().decode(res.body)\n    return JSON.parse(body).embedding\n} catch (e) {\n    throw new Error('An invalid response was returned by Bedrock.')\n}\n\n// after\nconst body = new TextDecoder().decode(res.body)\nlet parsed: any\ntry { parsed = JSON.parse(body) } catch (e) {\n    throw new Error(`Bedrock returned non-JSON for model ${model}: ${body.slice(0, 200)}`)\n}\nif (!parsed || !Array.isArray(parsed.embedding)) {\n    throw new Error(`Bedrock response missing 'embedding' for model ${model}: ${body.slice(0, 200)}`)\n}\nreturn parsed.embedding","handlingStrategy":"try-catch","validationCode":"import { BedrockRuntimeClient } from '@aws-sdk/client-bedrock-runtime'\n// pre-check the model is an embed model enabled in region\nif (!/titan-embed/i.test(model)) throw new Error(`${model} is not a Titan embedding model`)","typeGuard":"interface TitanEmbedResponse { embedding: number[] }\nfunction isTitanEmbedResponse(v: unknown): v is TitanEmbedResponse {\n    return !!v && Array.isArray((v as any).embedding)\n}","tryCatchPattern":"const body = new TextDecoder().decode(res.body)\nlet parsed: any\ntry { parsed = JSON.parse(body) } catch (e) {\n    throw new Error(`Titan embed: non-JSON body from Bedrock (model=${model}): ${body.slice(0, 200)}`)\n}\nif (!isTitanEmbedResponse(parsed)) {\n    throw new Error(`Titan embed: response missing 'embedding' (model=${model}): ${body.slice(0, 200)}`)\n}\nreturn parsed.embedding","preventionTips":["Confirm modelId is a Titan embed model and is enabled in the region.","Verify IAM bedrock:InvokeModel permission for the model.","Pin the AWS SDK version; retest when upgrading.","Log the raw response body when shape validation fails."],"tags":["aws-bedrock","titan","embeddings","error-masking","response-parsing"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}