Skip to content

LanguageModel

Defines the shared service for language model providers.

The LanguageModel service lets application code ask for generated text, streamed text, or structured output without depending on a specific provider. Requests can include tools, and the service can resolve tool calls while the model is generating a response. This module contains the service contract, request and response types, structured-output support, and the constructor used by provider packages to adapt their own generate and stream functions to the shared interface.

19 exports Added in v4.0.0 Source

Constructors

make

Added in v4.0.0 Source

Creates a LanguageModel service from provider-specific text generation and streaming implementations.

When to use

Use when you are implementing a provider adapter and need to expose the standard language-model service while keeping provider-specific request hooks behind it.

Details

The returned service implements generateText, generateObject, and streamText. It prepares ProviderOptions for each request, including the normalized prompt, tools, tool choice, response format, tracing span, and incremental response fields, before calling the supplied provider hook. Structured object generation uses the generateText hook and the configured codecTransformer, or defaultCodecTransformer when none is supplied.

Gotchas

Provider hooks must return encoded response parts that match the toolkit and response format prepared in ProviderOptions; invalid parts fail decoding as AiError.InvalidOutputError.

See

Signature

declare const make: (params: {
  readonly codecTransformer?: CodecTransformer;
  readonly generateText: (
    options: ProviderOptions,
  ) => Effect.Effect<Array<Response.PartEncoded>, AiError.AiError, IdGenerator>;
  readonly streamText: (
    options: ProviderOptions,
  ) => Stream.Stream<Response.StreamPartEncoded, AiError.AiError, IdGenerator>;
}) => Effect.Effect<Service>;

Generators

Generates a structured object from a schema using a language model.

Signature

declare function generateObject<
  ObjectEncoded extends Record<string, any>,
  StructuredOutputSchema extends Encoder<ObjectEncoded, unknown>,
  Options extends NoExcessProperties<GenerateObjectOptions<any, StructuredOutputSchema>, Options>,
>(
  options: Options & GenerateObjectOptions<ExtractTools<Options>, StructuredOutputSchema>,
): Effect<
  GenerateObjectResponse<ExtractTools<Options>, StructuredOutputSchema["Type"]>,
  ExtractError<Options>,
  LanguageModel | ExtractServices<Options> | StructuredOutputSchema["DecodingServices"]
>;

Models

Response class for structured object generation operations.

Signature

declare class GenerateObjectResponse<Tools extends Record<string, Tool.Any>, A> extends GenerateTextResponse<Tools> {
  constructor<Tools extends Record<string, Any>, A>(value: A, content: Array<Part<Tools>>);
  readonly value: A;
}

Response class for text generation operations, with accessors for extracting text, tool calls, usage information, and other response parts from generated content.

Signature

declare class GenerateTextResponse<Tools extends Record<string, Tool.Any>> {
  constructor<Tools extends Record<string, Any>>(content: Array<Part<Tools>>);
  readonly content: Array<Part<Tools>>;
  finishReason: "length" | "error" | "stop" | "content-filter" | "tool-calls" | "pause" | "other" | "unknown";
  reasoning: Array<ReasoningPart>;
  reasoningText: string | undefined;
  text: string;
  toolCalls: Array<ToolCallParts<Tools>>;
  toolResults: Array<ToolResultParts<Tools>>;
  usage: Usage;
}

Service interface

Added in v4.0.0 Source

The service interface for language model operations, defining the contract that all language model implementations must fulfill.

Signature

interface Service {
  readonly generateObject: <
    ObjectEncoded extends Record<string, any>,
    StructuredOutputSchema extends Encoder<ObjectEncoded, unknown>,
    Options extends NoExcessProperties<GenerateObjectOptions<any, StructuredOutputSchema>, Options>,
    Tools extends Record<string, Any> = {},
  >(
    options: Options & GenerateObjectOptions<Tools, StructuredOutputSchema>,
  ) => Effect<
    GenerateObjectResponse<Tools, StructuredOutputSchema["Type"]>,
    ExtractError<Options>,
    ExtractServices<Options> | StructuredOutputSchema["DecodingServices"]
  >;
  readonly generateText: {
    <Options extends NoExcessProperties<GenerateTextOptionsWithoutToolkit, Options>>(
      options: Options &
        Omit<GenerateTextOptions<{}>, "toolkit"> & {
          readonly toolkit?: undefined;
        },
    ): Effect<GenerateTextResponse<{}>, ExtractError<Options>, ExtractServices<Options>>;
    <
      Tools extends Record<string, Any>,
      Options extends NoExcessProperties<
        GenerateTextOptions<Tools> & {
          readonly toolkit: ToolkitInput<Tools>;
        },
        Options
      >,
    >(
      options: Options &
        GenerateTextOptions<Tools> & {
          readonly toolkit: ToolkitInput<Tools>;
        },
    ): Effect<GenerateTextResponse<Tools>, ExtractError<Options>, ExtractServices<Options>>;
    <
      Options extends {
        readonly toolkit: WithHandler<any> | Effect<WithHandler<any>, never, any>;
      } & GenerateTextOptions<any> &
        Readonly<Record<Exclude<keyof Options, keyof GenerateTextOptions<any>>, never>>,
    >(
      options: Options &
        GenerateTextOptions<ExtractTools<Options>> & {
          readonly toolkit: Options["toolkit"];
        },
    ): Effect<
      GenerateTextResponse<ExtractTools<Options>>,
      ExtractError<Options>,
      ExtractServices<Options>
    >;
  };
  readonly streamText: {
    <Options extends NoExcessProperties<GenerateTextOptionsWithoutToolkit, Options>>(
      options: Options &
        Omit<GenerateTextOptions<{}>, "toolkit"> & {
          readonly toolkit?: undefined;
        },
    ): Stream<StreamPart<{}>, ExtractError<Options>, ExtractServices<Options>>;
    <
      Tools extends Record<string, Any>,
      Options extends NoExcessProperties<
        GenerateTextOptions<Tools> & {
          readonly toolkit: ToolkitInput<Tools>;
        },
        Options
      >,
    >(
      options: Options &
        GenerateTextOptions<Tools> & {
          readonly toolkit: ToolkitInput<Tools>;
        },
    ): Stream<StreamPart<Tools>, ExtractError<Options>, ExtractServices<Options>>;
    <
      Options extends {
        readonly toolkit: WithHandler<any> | Effect<WithHandler<any>, never, any>;
      } & GenerateTextOptions<any> &
        Readonly<Record<Exclude<keyof Options, keyof GenerateTextOptions<any>>, never>>,
    >(
      options: Options &
        GenerateTextOptions<ExtractTools<Options>> & {
          readonly toolkit: Options["toolkit"];
        },
    ): Stream<StreamPart<ExtractTools<Options>>, ExtractError<Options>, ExtractServices<Options>>;
  };
}

ToolChoice type

Added in v4.0.0 Source

The tool choice mode for the language model. - auto (default): The model can decide whether or not to call tools, as well as which tools to call. - required: The model must call a tool but can decide which tool will be called. - none: The model must not call a tool. - { tool: <tool_name> }: The model must call the specified tool. - { mode?: "auto" (default) | "required", "oneOf": [<tool-names>] }: The model is restricted to the subset of tools specified by oneOf. When mode is "auto" or omitted, the model can decide whether or not a tool from the allowed subset of tools can be called. When mode is "required", the model must call one tool from the allowed subset of tools.

Signature

type ToolChoice<ToolName extends string> =
  | "auto"
  | "none"
  | "required"
  | {
      readonly tool: ToolName;
    }
  | {
      readonly mode?: "auto" | "required";
      readonly oneOf: ReadonlyArray<ToolName>;
    };

Options

GenerateObjectOptions interface

Added in v4.0.0 Source

Configuration options for structured object generation.

Signature

interface GenerateObjectOptions<
  Tools extends Record<string, Tool.Any>,
  StructuredOutputSchema extends Schema.Top,
> extends GenerateTextOptions<Tools> {
  readonly objectName?: string;
  readonly schema: StructuredOutputSchema;
}

GenerateTextOptions interface

Added in v4.0.0 Source

Configuration options for text generation.

Signature

interface GenerateTextOptions<Tools extends Record<string, Tool.Any>> {
  readonly concurrency?: Concurrency;
  readonly disableToolCallResolution?: boolean;
  readonly prompt: RawInput;
  readonly toolChoice?: ToolChoice<
    { [Name in string | number | symbol]: Tools[Name]["name"] }[keyof Tools]
  >;
  readonly toolkit?: ToolkitInput<Tools, never, any>;
}

ProviderOptions interface

Added in v4.0.0 Source

Configuration options passed along to language model provider implementations.

Details

This interface defines the normalized options that are passed to the underlying provider implementation, regardless of the specific provider being used.

Signature

interface ProviderOptions {
  readonly incrementalPrompt: Prompt | undefined;
  readonly previousResponseId: string | undefined;
  readonly prompt: Prompt;
  readonly responseFormat: {
    readonly type: "text";
  } | {
    readonly objectName: string;
    readonly schema: Top;
    readonly type: "json";
  };
  readonly span: Span;
  readonly toolChoice: ToolChoice<any>;
  readonly tools: readonly Array<Any>;
}

Services

The default codec transformer that passes schemas through without provider-specific rewrites.

When to use

Use as the codec transformer for provider implementations when the provider accepts the JSON Schema generated from an Effect Schema codec without provider-specific rewrites.

Details

The transformer returns the original codec, resolves a top-level $ref, and copies schema definitions into $defs.

See

  • CodecTransformer for the structured-output transformer contract
  • make for where this transformer is used as the default

Signature

declare const defaultCodecTransformer: CodecTransformer;

Service tag for AI model services.

When to use

Use to access or provide text generation, streaming generation, structured output, and tool-calling capabilities through the Effect context.

Signature

declare class LanguageModel extends Shape<"effect/unstable/ai/LanguageModel", Service, this> {
  constructor(_: never);
}

Text Generation

generateText

Added in v4.0.0 Source

Generates text using a language model.

Signature

declare const generateText: {
  <Options extends NoExcessProperties<GenerateTextOptionsWithoutToolkit, Options>>(
    options: Options &
      Omit<GenerateTextOptions<{}>, "toolkit"> & {
        readonly toolkit?: undefined;
      },
  ): Effect<
    GenerateTextResponse<{}>,
    ExtractError<Options>,
    LanguageModel | ExtractServices<Options>
  >;
  <
    Tools extends Record<string, Any>,
    Options extends NoExcessProperties<
      GenerateTextOptions<Tools> & {
        readonly toolkit: ToolkitInput<Tools>;
      },
      Options
    >,
  >(
    options: Options &
      GenerateTextOptions<Tools> & {
        readonly toolkit: ToolkitInput<Tools>;
      },
  ): Effect<
    GenerateTextResponse<Tools>,
    ExtractError<Options>,
    LanguageModel | ExtractServices<Options>
  >;
  <
    Options extends {
      readonly toolkit: ToolkitOption<any>;
    } & GenerateTextOptions<any> &
      Readonly<Record<Exclude<keyof Options, keyof GenerateTextOptions<any>>, never>>,
  >(
    options: Options &
      GenerateTextOptions<ExtractTools<Options>> & {
        readonly toolkit: Options["toolkit"];
      },
  ): Effect<
    GenerateTextResponse<ExtractTools<Options>>,
    ExtractError<Options>,
    LanguageModel | ExtractServices<Options>
  >;
};

streamText

Added in v4.0.0 Source

Generates text using a language model with streaming output.

Details

Returns a stream of response parts that are emitted as soon as they are available from the model, enabling real-time text generation experiences.

Signature

declare const streamText: {
  <Options extends NoExcessProperties<GenerateTextOptionsWithoutToolkit, Options>>(
    options: Options &
      Omit<GenerateTextOptions<{}>, "toolkit"> & {
        readonly toolkit?: undefined;
      },
  ): Stream<StreamPart<{}>, ExtractError<Options>, LanguageModel | ExtractServices<Options>>;
  <
    Tools extends Record<string, Any>,
    Options extends NoExcessProperties<
      GenerateTextOptions<Tools> & {
        readonly toolkit: ToolkitInput<Tools>;
      },
      Options
    >,
  >(
    options: Options &
      GenerateTextOptions<Tools> & {
        readonly toolkit: ToolkitInput<Tools>;
      },
  ): Stream<StreamPart<Tools>, ExtractError<Options>, LanguageModel | ExtractServices<Options>>;
  <
    Options extends {
      readonly toolkit: ToolkitOption<any>;
    } & GenerateTextOptions<any> &
      Readonly<Record<Exclude<keyof Options, keyof GenerateTextOptions<any>>, never>>,
  >(
    options: Options &
      GenerateTextOptions<ExtractTools<Options>> & {
        readonly toolkit: Options["toolkit"];
      },
  ): Stream<
    StreamPart<ExtractTools<Options>>,
    ExtractError<Options>,
    LanguageModel | ExtractServices<Options>
  >;
};

Utility Types

CodecTransformer type

Added in v4.0.0 Source

A function that transforms a Schema.Codec into a provider-compatible form for structured output generation.

Details

Different language model providers have varying constraints on the JSON schemas they accept. A CodecTransformer rewrites a codec's encoded side to satisfy those constraints while preserving the decoded type. A provider schema may be less restrictive than the codec when the provider cannot express every constraint; the returned codec remains authoritative for validating model output.

Signature

type CodecTransformer = <T, E, RD, RE>(
  schema: Schema.ConstraintCodec<T, E, RD, RE>,
) => {
  readonly codec: Schema.ConstraintCodec<T, unknown, RD, RE>;
  readonly jsonSchema: JsonSchema.JsonSchema;
};

ExtractError type

Added in v4.0.0 Source

Utility type that extracts the error type from LanguageModel options.

Details

Automatically infers the possible error types based on toolkit configuration and tool call resolution settings.

Signature

type ExtractError<Options> = Options extends {
  readonly disableToolCallResolution: true;
  readonly toolkit: infer ToolkitValue;
}
  ? ExtractErrorFromToolkitOption<Exclude<ToolkitValue, undefined>, true>
  : Options extends {
        readonly toolkit: infer ToolkitValue;
      }
    ? ExtractErrorFromToolkitOption<Exclude<ToolkitValue, undefined>, false>
    : Options extends {
          readonly disableToolCallResolution: true;
        }
      ? AiError.AiError
      : AiError.AiError;

ExtractServices type

Added in v4.0.0 Source

Utility type that extracts the context requirements from LanguageModel options.

Details

Automatically infers the required services based on the toolkit configuration.

Signature

type ExtractServices<Options> = Options extends {
  readonly disableToolCallResolution: true;
}
  ? never
  : Options extends {
        readonly toolkit: infer Toolkit;
      }
    ? ExtractServicesFromToolkitOption<Exclude<Toolkit, undefined>>
    : never;

ExtractTools type

Added in v4.0.0 Source

Utility type that extracts the toolset from LanguageModel options.

Signature

type ExtractTools<Options> = Options extends {
  readonly toolkit: infer ToolkitValue;
}
  ? ExtractToolsFromToolkitOption<Exclude<ToolkitValue, undefined>>
  : {};

ToolkitInput type

Added in v4.0.0 Source

The supported toolkit input shapes for language model operation options.

Details

Unlike ToolkitOption, this type does not distribute over unions. It is intended for call-site assignability, while ToolkitOption remains the distributive helper used for extraction and inference.

Signature

type ToolkitInput<Tools extends Record<string, Tool.Any>, E = never, R = any> =
  | ToolkitOption<Tools, E, R>
  | Toolkit.WithHandler<Tools>
  | Effect.Effect<Toolkit.WithHandler<Tools>, E, R>;

ToolkitOption type

Added in v4.0.0 Source

The supported toolkit option shapes for language model operations.

Signature

type ToolkitOption<Tools extends Record<string, Tool.Any>, E = never, R = any> = Tools extends any
  ? Toolkit.WithHandler<Tools> | Effect.Effect<Toolkit.WithHandler<Tools>, E, R>
  : never;