langchain.js
    Preparing search index...

    Function providerStrategy

    • Creates a provider strategy for structured output using native JSON schema support.

      This function is used to configure structured output for agents when the underlying model supports native JSON schema output (e.g., OpenAI's gpt-4o, gpt-4o-mini, and newer models). Unlike toolStrategy, which uses function calling to extract structured output, providerStrategy leverages the provider's native structured output capabilities, resulting in more efficient and reliable schema enforcement.

      When used with a model that supports JSON schema output, the model will return responses that directly conform to the provided schema without requiring tool calls. This is the recommended approach for structured output when your model supports it.

      Type Parameters

      • T extends InteropZodType<unknown>

      Parameters

      • responseFormat: T | { schema: T; strict?: boolean }

        The schema to enforce, either a Zod schema, a JSON schema object, or an options object with schema and optional strict flag

      Returns ProviderStrategy<T extends InteropZodType<U> ? U : never>

      A ProviderStrategy instance that can be used as the responseFormat in createAgent

      import { providerStrategy, createAgent } from "langchain";
      import { z } from "zod";

      const agent = createAgent({
      model: "claude-haiku-4-5",
      responseFormat: providerStrategy(
      z.object({
      answer: z.string().describe("The answer to the question"),
      confidence: z.number().min(0).max(1),
      })
      ),
      });
      // Using strict mode for stricter schema enforcement
      const agent = createAgent({
      model: "claude-haiku-4-5",
      responseFormat: providerStrategy({
      schema: z.object({
      name: z.string(),
      age: z.number(),
      }),
      strict: true
      }),
      });
    • Creates a provider strategy for structured output using native JSON schema support.

      This function is used to configure structured output for agents when the underlying model supports native JSON schema output (e.g., OpenAI's gpt-4o, gpt-4o-mini, and newer models). Unlike toolStrategy, which uses function calling to extract structured output, providerStrategy leverages the provider's native structured output capabilities, resulting in more efficient and reliable schema enforcement.

      When used with a model that supports JSON schema output, the model will return responses that directly conform to the provided schema without requiring tool calls. This is the recommended approach for structured output when your model supports it.

      Parameters

      • responseFormat: JsonSchemaFormat | { schema: JsonSchemaFormat; strict?: boolean }

        The schema to enforce, either a Zod schema, a JSON schema object, or an options object with schema and optional strict flag

      Returns ProviderStrategy<Record<string, unknown>>

      A ProviderStrategy instance that can be used as the responseFormat in createAgent

      import { providerStrategy, createAgent } from "langchain";
      import { z } from "zod";

      const agent = createAgent({
      model: "claude-haiku-4-5",
      responseFormat: providerStrategy(
      z.object({
      answer: z.string().describe("The answer to the question"),
      confidence: z.number().min(0).max(1),
      })
      ),
      });
      // Using strict mode for stricter schema enforcement
      const agent = createAgent({
      model: "claude-haiku-4-5",
      responseFormat: providerStrategy({
      schema: z.object({
      name: z.string(),
      age: z.number(),
      }),
      strict: true
      }),
      });