Documentation

Build LLM-powered agents
with production-ready TypeScript

DSPy for TypeScript. Working with LLMs is complex—they don't always do what you want. DSPy makes it easier to build amazing things with LLMs. Just define your inputs and outputs (signature) and an efficient prompt is auto-generated and used. Connect together various signatures to build complex systems and workflows using LLMs.

15+ LLM Providers
End-to-end Streaming
Auto Prompt Tuning

AxDefaultResultReranker

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/docs/reranker.ts#L11

Extends

Constructors

Constructor

new AxDefaultResultReranker(options?: Readonly<AxProgramForwardOptions<string>>): AxDefaultResultReranker;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/docs/reranker.ts#L15

Parameters

ParameterType
options?Readonly<AxProgramForwardOptions<string>>

Returns

AxDefaultResultReranker

Overrides

AxGen.constructor

Methods

_forward1()

_forward1(
   ai: Readonly<AxAIService>, 
   values: 
  | AxRerankerIn
  | AxMessage<AxRerankerIn>[], 
options: Readonly<AxProgramForwardOptions<any>>): AxGenStreamingOut<AxRerankerOut>;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L1288

Parameters

ParameterType
aiReadonly<AxAIService>
values| AxRerankerIn | AxMessage<AxRerankerIn>[]
optionsReadonly<AxProgramForwardOptions<any>>

Returns

AxGenStreamingOut<AxRerankerOut>

Inherited from

AxGen._forward1


addAssert()

addAssert(fn: (values: AxRerankerOut) => 
  | undefined
  | string
  | boolean
  | Promise<undefined | string | boolean>, message?: string): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L230

Parameters

ParameterType
fn(values: AxRerankerOut) => | undefined | string | boolean | Promise<undefined | string | boolean>
message?string

Returns

void

Inherited from

AxGen.addAssert


addFieldProcessor()

addFieldProcessor(fieldName: "rankedItems", fn: (value: string[], context?: object) => unknown): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L305

Parameters

ParameterType
fieldName"rankedItems"
fn(value: string[], context?: object) => unknown

Returns

void

Inherited from

AxGen.addFieldProcessor


addStreamingAssert()

addStreamingAssert(
   fieldName: "rankedItems", 
   fn: (content: string, done?: boolean) => undefined | string | boolean, 
   message?: string): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L234

Parameters

ParameterType
fieldName"rankedItems"
fn(content: string, done?: boolean) => undefined | string | boolean
message?string

Returns

void

Inherited from

AxGen.addStreamingAssert


addStreamingFieldProcessor()

addStreamingFieldProcessor(fieldName: "rankedItems", fn: (value: string, context?: object) => unknown): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L291

Parameters

ParameterType
fieldName"rankedItems"
fn(value: string, context?: object) => unknown

Returns

void

Inherited from

AxGen.addStreamingFieldProcessor


applyOptimization()

applyOptimization(optimizedProgram: AxOptimizedProgram<AxRerankerOut>): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L213

Apply optimized configuration to this program

Parameters

ParameterTypeDescription
optimizedProgramAxOptimizedProgram<AxRerankerOut>The optimized program configuration to apply

Returns

void

Inherited from

AxGen.applyOptimization


clone()

clone(): AxGen<AxRerankerIn, AxRerankerOut>;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L138

Returns

AxGen<AxRerankerIn, AxRerankerOut>

Inherited from

AxGen.clone


forward()

forward<T>(
   ai: T, 
   input: Readonly<AxRerankerIn>, 
options?: Readonly<AxProgramForwardOptionsWithModels<T>>): Promise<AxRerankerOut>;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/docs/reranker.ts#L22

Executes the generator with the given AI service and input values.

This is the main entry point for running an AI generation. The execution pipeline:

  1. Validate - Check input values match the signature
  2. Render - Build the prompt from signature, examples, and inputs
  3. Call - Send the request to the AI service
  4. Parse - Extract structured outputs from the response
  5. Assert - Validate outputs and retry with error correction if needed

Type Parameters

Type Parameter
T extends Readonly<AxAIService<unknown, unknown, string>>

Parameters

ParameterTypeDescription
aiTThe AI service instance to use (created via ai() factory)
inputReadonly<AxRerankerIn>Input values matching the signature’s input fields, or an array of AxMessage objects for multi-turn conversations
options?Readonly<AxProgramForwardOptionsWithModels<T>>Optional execution configuration

Returns

Promise<AxRerankerOut>

Promise resolving to the output values matching the signature’s output fields

Throws

When input values don’t match the signature

Throws

When output parsing/validation fails after all retries

Throws

When the AI service request fails

Examples

const gen = ax('question: string -> answer: string');
const result = await gen.forward(ai, { question: 'What is 2+2?' });
console.log(result.answer); // "4"
const result = await gen.forward(ai, { question: 'Explain quantum computing' }, {
  maxTokens: 2000,
  temperature: 0.3,
  stream: true
});
const mem = new AxMemory();
const chat = ax('message: string -> reply: string');

await chat.forward(ai, { message: 'Hi, my name is Alice' }, { mem });
const result = await chat.forward(ai, { message: 'What is my name?' }, { mem });
// result.reply will reference "Alice" from conversation history
const result = await gen.forward(ai, values, {
  functions: [{
    name: 'getWeather',
    description: 'Get current weather for a city',
    parameters: {
      type: 'object',
      properties: { city: { type: 'string', description: 'City name' } },
      required: ['city']
    },
    func: async ({ city }) => fetchWeather(city)
  }],
  maxSteps: 5
});

Overrides

AxGen.forward


getInstruction()

getInstruction(): undefined | string;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L186

Returns

undefined | string

Inherited from

AxGen.getInstruction


getSignature()

getSignature(): AxSignature;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L56

Returns

AxSignature

Inherited from

AxGen.getSignature


getTraces()

getTraces(): AxProgramTrace<AxRerankerIn, AxRerankerOut>[];

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L154

Returns

AxProgramTrace<AxRerankerIn, AxRerankerOut>[]

Inherited from

AxGen.getTraces


getUsage()

getUsage(): AxModelUsage & object[];

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L168

Returns

AxModelUsage & object[]

Inherited from

AxGen.getUsage


register()

register(prog: Readonly<AxTunable<IN, OUT> & AxUsable>): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L84

Parameters

ParameterType
progReadonly<AxTunable<IN, OUT> & AxUsable>

Returns

void

Inherited from

AxGen.register


resetUsage()

resetUsage(): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L178

Returns

void

Inherited from

AxGen.resetUsage


setDemos()

setDemos(demos: readonly AxProgramDemos<AxRerankerIn, AxRerankerOut>[]): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L185

Parameters

ParameterType
demosreadonly AxProgramDemos<AxRerankerIn, AxRerankerOut>[]

Returns

void

Inherited from

AxGen.setDemos


setDescription()

setDescription(description: string): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L74

Parameters

ParameterType
descriptionstring

Returns

void

Inherited from

AxGen.setDescription


setExamples()

setExamples(examples: Readonly<AxProgramExamples<AxRerankerIn, AxRerankerOut>>, options?: Readonly<AxSetExamplesOptions>): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L1742

Parameters

ParameterType
examplesReadonly<AxProgramExamples<AxRerankerIn, AxRerankerOut>>
options?Readonly<AxSetExamplesOptions>

Returns

void

Inherited from

AxGen.setExamples


setId()

setId(id: string): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L91

Parameters

ParameterType
idstring

Returns

void

Inherited from

AxGen.setId


setInstruction()

setInstruction(instruction: string): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L182

Parameters

ParameterType
instructionstring

Returns

void

Inherited from

AxGen.setInstruction


setParentId()

setParentId(parentId: string): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L98

Parameters

ParameterType
parentIdstring

Returns

void

Inherited from

AxGen.setParentId


setSignature()

setSignature(signature: 
  | undefined
  | Readonly<
  | string
  | AxSignatureConfig
  | AxSignature<Record<string, any>, Record<string, any>>>): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/program.ts#L60

Parameters

ParameterType
signature| undefined | Readonly< | string | AxSignatureConfig | AxSignature<Record<string, any>, Record<string, any>>>

Returns

void

Inherited from

AxGen.setSignature


streamingForward()

streamingForward<T>(
   ai: T, 
   values: 
  | AxRerankerIn
  | AxMessage<AxRerankerIn>[], 
options?: Readonly<AxProgramStreamingForwardOptionsWithModels<T>>): AxGenStreamingOut<AxRerankerOut>;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L1653

Type Parameters

Type Parameter
T extends Readonly<AxAIService<unknown, unknown, string>>

Parameters

ParameterType
aiT
values| AxRerankerIn | AxMessage<AxRerankerIn>[]
options?Readonly<AxProgramStreamingForwardOptionsWithModels<T>>

Returns

AxGenStreamingOut<AxRerankerOut>

Inherited from

AxGen.streamingForward


updateMeter()

updateMeter(meter?: Meter): void;

Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/generate.ts#L210

Parameters

ParameterType
meter?Meter

Returns

void

Inherited from

AxGen.updateMeter