{"record":{"id":"3edb82daaebeb287","repo":"openclaw/openclaw","slug":"amazon-bedrock-embedding-response-returned-malform","errorCode":null,"errorMessage":"Amazon Bedrock embedding response returned malformed JSON","messagePattern":"Amazon Bedrock embedding response returned malformed JSON","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"extensions/amazon-bedrock/embedding-provider.ts","lineNumber":228,"sourceCode":"  }\n  return JSON.stringify(body);\n}\n\n// ---------------------------------------------------------------------------\n// Response parsers\n// ---------------------------------------------------------------------------\n\ntype BedrockEmbeddingResponseJson = {\n  embedding?: unknown;\n  embeddings?: unknown;\n  data?: unknown;\n};\n\nfunction parseBedrockEmbeddingResponseJson(raw: string): BedrockEmbeddingResponseJson {\n  try {\n    const parsed = JSON.parse(raw) as unknown;\n    if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n      throw new Error(\"Amazon Bedrock embedding response returned malformed JSON\");\n    }\n    return parsed as BedrockEmbeddingResponseJson;\n  } catch {\n    throw new Error(\"Amazon Bedrock embedding response returned malformed JSON\");\n  }\n}\n\nfunction malformedBedrockEmbeddingResponse(): Error {\n  return new Error(\"Amazon Bedrock embedding response returned malformed JSON\");\n}\n\nfunction asNumberArray(value: unknown): number[] {\n  if (!Array.isArray(value)) {\n    throw malformedBedrockEmbeddingResponse();\n  }\n  for (const entry of value) {\n    if (typeof entry !== \"number\" || !Number.isFinite(entry)) {\n      throw malformedBedrockEmbeddingResponse();","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/openclaw/openclaw/blob/01804a75319da4b69c9ab98ceaa30477e22b8c0b/extensions/amazon-bedrock/embedding-provider.ts#L210-L246","documentation":"Thrown by parseBedrockEmbeddingResponseJson when JSON.parse succeeds but the result is not a plain object (it is null, a primitive, or an array). Bedrock InvokeModel embedding responses must be a JSON object containing an 'embedding'/'embeddings'/'data' field, so any other shape is rejected as malformed.","triggerScenarios":"The Bedrock InvokeModel call returns a body that parses as valid JSON but is an array, a bare string/number, or null. This happens when a non-embedding model is invoked through the embedding path, or a proxy returns an unexpected JSON envelope.","commonSituations":"Misconfigured embedding model id pointing to a text-generation model whose response body is a different shape; a Bedrock proxy/gateway returning a list response; or a model region mismatch causing a different response contract.","solutions":["Verify the configured embedding model id is actually an embedding model (e.g. amazon.titan-embed-text-v1, cohere.embed-english-v3).","Capture the raw response body and inspect its shape to identify what the endpoint actually returned.","Check for a custom baseUrl/proxy that may be transforming the response.","Ensure the Bedrock region supports the configured model."],"exampleFix":"// Log the raw body before parsing to diagnose\nconsole.debug(\"Raw Bedrock embedding response:\", raw);\nconst parsed = JSON.parse(raw);\nif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n  throw new Error(\"unexpected shape: \" + JSON.stringify(parsed));\n}","handlingStrategy":"validation","validationCode":"// Pre-validate the expected shape before the library parses it\nfunction isValidEmbeddingResponseEnvelope(raw: string): boolean {\n  try {\n    const parsed = JSON.parse(raw);\n    return parsed !== null && typeof parsed === \"object\" && !Array.isArray(parsed);\n  } catch {\n    return false;\n  }\n}","typeGuard":"function isBedrockEmbeddingEnvelope(v: unknown): v is { embedding?: unknown; embeddings?: unknown; data?: unknown } {\n  return v !== null && typeof v === \"object\" && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  parseBedrockEmbeddingResponseJson(raw);\n} catch (err) {\n  if (err instanceof Error && err.message.includes(\"malformed JSON\")) {\n    logger.error(\"Unexpected Bedrock response shape\", { preview: raw.slice(0, 200) });\n  }\n  throw err;\n}","preventionTips":["Confirm the configured model id is an embedding model, not a text-generation model.","Log raw response bodies during development to catch shape mismatches early.","Verify no proxy is transforming the Bedrock response envelope."],"tags":["bedrock","embedding","response-parsing","json"],"backgroundTag":null,"analyzedSha":"01804a75319da4b69c9ab98ceaa30477e22b8c0b","analyzedAt":"2026-08-12T04:37:58.197Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}