#11730·langchainjs

ChatBedrockConverse.withStructuredOutput does not validate output against a zod schema

Author: jebediah47Created Sep 25, 2026Updated Sep 25, 2026

Description

ChatBedrockConverse.withStructuredOutput(zodSchema) returns the model's tool-call arguments without validating them against the schema. Invalid output reaches the caller as if it were a typed result.

Other chat model integrations behave differently. @langchain/openai and @langchain/anthropic build their parsers with createFunctionCallingParser / createContentParser from @langchain/core. For a zod schema, those parsers validate the result and reject a mismatch.

With the same schema and the same invalid model answer ({ "priority": "whenever" }, mocked at the HTTP/SDK layer):

Integration Result
@langchain/anthropic 1.5.11, default method throws OutputParserException
@langchain/openai 1.5.13, method: "functionCalling" throws OutputParserException
@langchain/openai 1.5.13, method: "jsonSchema" throws ZodError
@langchain/aws 1.4.5 (ChatBedrockConverse) returns { priority: "whenever" }

@langchain/aws uses its own parser instead (libs/providers/langchain-aws/src/chat_models.ts, withStructuredOutput, unchanged on main as of 2026-09-23):

typescript
const outputParser = RunnableLambda.from<AIMessageChunk, RunOutput>((input) => {
  if (!input.tool_calls || input.tool_calls.length === 0) throw new Error("No tool calls found in the response.");
  const toolCall = input.tool_calls.find((tc) => tc.name === functionName);
  if (!toolCall) throw new Error(`No tool call found with name ${functionName}.`);
  return toolCall.args as RunOutput;
});

The schema is only converted to JSON Schema for the tool definition.

Reproduction

typescript
import { ChatBedrockConverse } from "@langchain/aws";
import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
import { z } from "zod";

const client = new BedrockRuntimeClient({ region: "eu-central-1" });
// Simulate a model answer that violates the schema.
client.send = (async () => ({
  output: {
    message: {
      role: "assistant",
      content: [{ toolUse: { toolUseId: "call-1", name: "ticket", input: { priority: "whenever" } } }],
    },
  },
  stopReason: "tool_use",
  usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
})) as typeof client.send;

const model = new ChatBedrockConverse({
  model: "eu.anthropic.claude-sonnet-4-5-20250929-v1:0",
  region: "eu-central-1",
  client,
});
const schema = z.object({ priority: z.enum(["low", "high"]) });
const result = await model.withStructuredOutput(schema, { name: "ticket" }).invoke("classify");

console.log(result); // { priority: "whenever" } — expected OutputParserException

Expected behavior

Consistent with the other providers: validate zod (and standard) schemas, and throw OutputParserException when the output does not match, as the tool-calling paths of @langchain/openai and @langchain/anthropic do. The inferred return type z.infer<typeof schema> should hold at runtime.

Suggested fix

Use the shared parser from @langchain/core, for example createFunctionCallingParser(schema, functionName), in place of the custom RunnableLambda. This also brings standard-schema support in line with other providers.

Environment

  • @langchain/aws 1.4.5
  • @langchain/core 1.2.12
  • zod 4.6.5
  • Bun 1.4.2 (also applies to Node; the code path is runtime-independent)

Source: langchain-ai/langchainjs