Genkit encapsulates a single Genkit instance including the Registry, ReflectionServer, FlowServer, and configuration.

Do not instantiate this class directly. Use genkit.

Registry keeps track of actions, flows, tools, and many other components. Reflection server exposes an API to inspect the registry and trigger executions of actions in the registry. Flow server exposes flows as HTTP endpoints for production use.

There may be multiple Genkit instances in a single codebase.

Implements

  • HasRegistry

Constructors

Properties

configuredEnvs: Set<string> = ...

Environments that have been configured (at minimum dev).

flows: Action<any, any, any>[] = []

List of flows that have been registered in this instance.

options: GenkitOptions

Developer-configured options.

registry: Registry

Registry instance that is exclusively modified by this Genkit instance.

Methods

  • Returns current action (or flow) invocation context. Can be used to access things like auth data set by HTTP server frameworks. If invoked outside of an action (e.g. flow or tool) will return undefined.

    Returns undefined | ActionContext

  • Creates evaluator action for the provided EvaluatorFn implementation.

    Type Parameters

    • DataPoint extends ZodObject<
          {
              context: ZodOptional<ZodArray<ZodUnknown, "many">>;
              input: ZodUnknown;
              output: ZodOptional<ZodUnknown>;
              reference: ZodOptional<ZodUnknown>;
              testCaseId: ZodOptional<ZodString>;
              traceIds: ZodOptional<ZodArray<ZodString, "many">>;
          },
          "strip",
          ZodTypeAny,
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
          DataPoint,
      > = ZodObject<
          {
              context: ZodOptional<ZodArray<ZodUnknown, "many">>;
              input: ZodUnknown;
              output: ZodOptional<ZodUnknown>;
              reference: ZodOptional<ZodUnknown>;
              testCaseId: ZodOptional<ZodString>;
              traceIds: ZodOptional<ZodArray<ZodString, "many">>;
          },
          "strip",
          ZodTypeAny,
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
      >
    • EvalDataPoint extends ZodObject<
          extendShape<
              {
                  context: ZodOptional<ZodArray<ZodUnknown, "many">>;
                  input: ZodUnknown;
                  output: ZodOptional<ZodUnknown>;
                  reference: ZodOptional<ZodUnknown>;
                  testCaseId: ZodOptional<ZodString>;
                  traceIds: ZodOptional<ZodArray<ZodString, "many">>;
              },
              { testCaseId: ZodString },
          >,
          "strip",
          ZodTypeAny,
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId: string;
              traceIds?: string[];
          },
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId: string;
              traceIds?: string[];
          },
          EvalDataPoint,
      > = ZodObject<
          extendShape<
              {
                  context: ZodOptional<ZodArray<ZodUnknown, "many">>;
                  input: ZodUnknown;
                  output: ZodOptional<ZodUnknown>;
                  reference: ZodOptional<ZodUnknown>;
                  testCaseId: ZodOptional<ZodString>;
                  traceIds: ZodOptional<ZodArray<ZodString, "many">>;
              },
              { testCaseId: ZodString },
          >,
          "strip",
          ZodTypeAny,
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId: string;
              traceIds?: string[];
          },
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId: string;
              traceIds?: string[];
          },
      >
    • EvaluatorOptions extends ZodType<any, any, any, EvaluatorOptions> = ZodTypeAny

    Parameters

    Returns EvaluatorAction<
        ZodObject<
            {
                context: ZodOptional<ZodArray<ZodUnknown, "many">>;
                input: ZodUnknown;
                output: ZodOptional<ZodUnknown>;
                reference: ZodOptional<ZodUnknown>;
                testCaseId: ZodOptional<ZodString>;
                traceIds: ZodOptional<ZodArray<ZodString, "many">>;
            },
            "strip",
            ZodTypeAny,
            {
                context?: unknown[];
                input?: unknown;
                output?: unknown;
                reference?: unknown;
                testCaseId?: string;
                traceIds?: string[];
            },
            {
                context?: unknown[];
                input?: unknown;
                output?: unknown;
                reference?: unknown;
                testCaseId?: string;
                traceIds?: string[];
            },
        >,
        ZodTypeAny,
    >

  • Defines and registers a custom model output formatter.

    Here's an example of a custom JSON output formatter:

    import { extractJson } from 'genkit/extract';

    ai.defineFormat(
    { name: 'customJson' },
    (schema) => {
    let instructions: string | undefined;
    if (schema) {
    instructions = `Output should be in JSON format and conform to the following schema:
    \`\`\`
    ${JSON.stringify(schema)}
    \`\`\`
    `;
    }
    return {
    parseChunk: (chunk) => extractJson(chunk.accumulatedText),
    parseMessage: (message) => extractJson(message.text),
    instructions,
    };
    }
    );

    const { output } = await ai.generate({
    prompt: 'Invent a menu item for a pirate themed restaurant.',
    output: { format: 'customJson', schema: MenuItemSchema },
    });

    Parameters

    • options: { name: string } & {
          constrained?: boolean;
          contentType?: string;
          format?: string;
          instructions?: string;
          schema?: Record<string, any>;
      }
    • handler: (
          schema?: any,
      ) => {
          instructions?: string;
          parseChunk?: (chunk: GenerateResponseChunk<unknown>) => unknown;
          parseMessage(message: Message<unknown>): unknown;
      }

    Returns {
        config:
            | undefined
            | {
                constrained?: boolean;
                contentType?: string;
                format?: string;
                instructions?: string;
                schema?: Record<string, any>;
            };
        handler: (
            schema?: any,
        ) => {
            instructions?: string;
            parseChunk?: (chunk: GenerateResponseChunk<unknown>) => unknown;
            parseMessage(message: Message<unknown>): unknown;
        };
    }

  • Defines and registers an interrupt.

    Interrupts are special tools that halt model processing and return control back to the caller. Interrupts make it simpler to implement "human-in-the-loop" and out-of-band processing patterns that require waiting on external actions to complete.

    Type Parameters

    Parameters

    Returns ToolAction<I, O>

  • Defines and registers a schema from a JSON schema.

    Defined schemas can be referenced by name in prompts in place of inline schemas.

    Parameters

    • name: string
    • jsonSchema: any

    Returns any

  • Defines a new model and adds it to the registry.

    Type Parameters

    Parameters

    • options: DefineModelOptions<CustomOptionsSchema>
    • runner: (
          request: GenerateRequest<CustomOptionsSchema>,
          streamingCallback?: StreamingCallback<
              {
                  aggregated?: boolean;
                  content: (
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text: string;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media: { contentType?: string; url: string };
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest: { input?: unknown; name: string; ref?: string };
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse: { name: string; output?: unknown; ref?: string };
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom: Record<string, any>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                  )[];
                  custom?: unknown;
                  index?: number;
                  role?: "user"
                  | "model"
                  | "tool"
                  | "system";
              },
          >,
      ) => Promise<
          {
              candidates?: {
                  custom?: unknown;
                  finishMessage?: string;
                  finishReason: | "length"
                  | "stop"
                  | "interrupted"
                  | "blocked"
                  | "unknown"
                  | "other";
                  index: number;
                  message: {
                      content: (
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text: string;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media: { contentType?: ...; url: ... };
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest: { input?: ...; name: ...; ref?: ... };
                              toolResponse?: undefined;
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse: { name: ...; output?: ...; ref?: ... };
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                          | {
                              custom: Record<(...), (...)>;
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                      )[];
                      metadata?: Record<string, unknown>;
                      role: "user" | "model" | "tool" | "system";
                  };
                  usage?: {
                      custom?: Record<string, number>;
                      inputAudioFiles?: number;
                      inputCharacters?: number;
                      inputImages?: number;
                      inputTokens?: number;
                      inputVideos?: number;
                      outputAudioFiles?: number;
                      outputCharacters?: number;
                      outputImages?: number;
                      outputTokens?: number;
                      outputVideos?: number;
                      totalTokens?: number;
                  };
              }[];
              custom?: unknown;
              finishMessage?: string;
              finishReason?: | "length"
              | "stop"
              | "interrupted"
              | "blocked"
              | "unknown"
              | "other";
              latencyMs?: number;
              message?: {
                  content: (
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text: string;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media: { contentType?: string; url: string };
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest: { input?: unknown; name: string; ref?: string };
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse: { name: string; output?: unknown; ref?: string };
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom: Record<string, any>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                  )[];
                  metadata?: Record<string, unknown>;
                  role: "user" | "model" | "tool" | "system";
              };
              raw?: unknown;
              request?: {
                  candidates?: number;
                  config?: any;
                  docs?: {
                      content: ({ media?: ...; text: ... } | { media: ...; text?: ... })[];
                      metadata?: Record<string, any>;
                  }[];
                  messages: {
                      content: (
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text: string;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media: { contentType?: ...; url: ... };
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest: { input?: ...; name: ...; ref?: ... };
                              toolResponse?: undefined;
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse: { name: ...; output?: ...; ref?: ... };
                          }
                          | {
                              custom?: (...)
                              | (...);
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                          | {
                              custom: Record<(...), (...)>;
                              data?: unknown;
                              media?: undefined;
                              metadata?: (...) | (...);
                              text?: undefined;
                              toolRequest?: undefined;
                              toolResponse?: undefined;
                          }
                      )[];
                      metadata?: Record<string, unknown>;
                      role: "user" | "model" | "tool" | "system";
                  }[];
                  output?: {
                      constrained?: boolean;
                      contentType?: string;
                      format?: string;
                      instructions?: string;
                      schema?: Record<string, any>;
                  };
                  toolChoice?: "required"
                  | "auto"
                  | "none";
                  tools?: {
                      description: string;
                      inputSchema?: null | Record<string, any>;
                      metadata?: Record<string, any>;
                      name: string;
                      outputSchema?: null | Record<string, any>;
                  }[];
              };
              usage?: {
                  custom?: Record<string, number>;
                  inputAudioFiles?: number;
                  inputCharacters?: number;
                  inputImages?: number;
                  inputTokens?: number;
                  inputVideos?: number;
                  outputAudioFiles?: number;
                  outputCharacters?: number;
                  outputImages?: number;
                  outputTokens?: number;
                  outputVideos?: number;
                  totalTokens?: number;
              };
          },
      >

    Returns ModelAction<CustomOptionsSchema>

  • Defines and registers a schema from a Zod schema.

    Defined schemas can be referenced by name in prompts in place of inline schemas.

    Type Parameters

    Parameters

    • name: string
    • schema: T

    Returns T

  • A veneer for interacting with embedder models in bulk.

    Type Parameters

    Parameters

    • params: {
          content:
              | string[]
              | {
                  content: (
                      | { media?: undefined; text: string }
                      | { media: { contentType?: string; url: string }; text?: undefined }
                  )[];
                  metadata?: Record<string, any>;
              }[];
          embedder: EmbedderArgument<ConfigSchema>;
          metadata?: Record<string, unknown>;
          options?: TypeOf<ConfigSchema>;
      }

    Returns Promise<EmbeddingBatch>

  • Evaluates the given dataset using the specified evaluator.

    Type Parameters

    • DataPoint extends ZodObject<
          {
              context: ZodOptional<ZodArray<ZodUnknown, "many">>;
              input: ZodUnknown;
              output: ZodOptional<ZodUnknown>;
              reference: ZodOptional<ZodUnknown>;
              testCaseId: ZodOptional<ZodString>;
              traceIds: ZodOptional<ZodArray<ZodString, "many">>;
          },
          "strip",
          ZodTypeAny,
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
          DataPoint,
      > = ZodObject<
          {
              context: ZodOptional<ZodArray<ZodUnknown, "many">>;
              input: ZodUnknown;
              output: ZodOptional<ZodUnknown>;
              reference: ZodOptional<ZodUnknown>;
              testCaseId: ZodOptional<ZodString>;
              traceIds: ZodOptional<ZodArray<ZodString, "many">>;
          },
          "strip",
          ZodTypeAny,
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
          {
              context?: unknown[];
              input?: unknown;
              output?: unknown;
              reference?: unknown;
              testCaseId?: string;
              traceIds?: string[];
          },
      >
    • CustomOptions extends ZodType<any, any, any, CustomOptions> = ZodTypeAny

    Returns Promise<
        {
            evaluation: | {
                details?: objectOutputType<
                    { reasoning: ZodOptional<ZodString> },
                    ZodTypeAny,
                    "passthrough",
                >;
                error?: string;
                id?: string;
                score?: string | number | boolean;
            }
            | {
                details?: objectOutputType<
                    { reasoning: ZodOptional<ZodString> },
                    ZodTypeAny,
                    "passthrough",
                >;
                error?: string;
                id?: string;
                score?: string | number | boolean;
            }[];
            sampleIndex?: number;
            spanId?: string;
            testCaseId: string;
            traceId?: string;
        }[],
    >

  • Make a generate call to the default model with a simple text prompt.

    const ai = genkit({
    plugins: [googleAI()],
    model: gemini15Flash, // default model
    })

    const { text } = await ai.generate('hi');

    Type Parameters

    Parameters

    • strPrompt: string

    Returns Promise<GenerateResponse<TypeOf<O>>>

  • Make a generate call to the default model with a multipart request.

    const ai = genkit({
    plugins: [googleAI()],
    model: gemini15Flash, // default model
    })

    const { text } = await ai.generate([
    { media: {url: 'http://....'} },
    { text: 'describe this image' }
    ]);

    Type Parameters

    Parameters

    • parts: (
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text: string;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media: { contentType?: string; url: string };
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest: { input?: unknown; name: string; ref?: string };
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse: { name: string; output?: unknown; ref?: string };
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom: Record<string, any>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
      )[]

    Returns Promise<GenerateResponse<TypeOf<O>>>

  • Generate calls a generative model based on the provided prompt and configuration. If messages is provided, the generation will include a conversation history in its request. If tools are provided, the generate method will automatically resolve tool calls returned from the model unless returnToolRequests is set to true.

    See GenerateOptions for detailed information about available options.

    const ai = genkit({
    plugins: [googleAI()],
    })

    const { text } = await ai.generate({
    system: 'talk like a pirate',
    prompt: [
    { media: { url: 'http://....' } },
    { text: 'describe this image' }
    ],
    messages: conversationHistory,
    tools: [ userInfoLookup ],
    model: gemini15Flash,
    });

    Type Parameters

    • O extends ZodType<any, any, any, O> = ZodTypeAny
    • CustomOptions extends ZodType<any, any, any, CustomOptions> = ZodObject<
          {
              maxOutputTokens: ZodOptional<ZodNumber>;
              stopSequences: ZodOptional<ZodArray<ZodString, "many">>;
              temperature: ZodOptional<ZodNumber>;
              topK: ZodOptional<ZodNumber>;
              topP: ZodOptional<ZodNumber>;
              version: ZodOptional<ZodString>;
          },
          "strip",
          ZodTypeAny,
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
      >

    Parameters

    Returns Promise<GenerateResponse<TypeOf<O>>>

  • Make a streaming generate call to the default model with a simple text prompt.

    const ai = genkit({
    plugins: [googleAI()],
    model: gemini15Flash, // default model
    })

    const { response, stream } = ai.generateStream('hi');
    for await (const chunk of stream) {
    console.log(chunk.text);
    }
    console.log((await response).text);

    Type Parameters

    Parameters

    • strPrompt: string

    Returns GenerateStreamResponse<TypeOf<O>>

  • Make a streaming generate call to the default model with a multipart request.

    const ai = genkit({
    plugins: [googleAI()],
    model: gemini15Flash, // default model
    })

    const { response, stream } = ai.generateStream([
    { media: {url: 'http://....'} },
    { text: 'describe this image' }
    ]);
    for await (const chunk of stream) {
    console.log(chunk.text);
    }
    console.log((await response).text);

    Type Parameters

    Parameters

    • parts: (
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text: string;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media: { contentType?: string; url: string };
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest: { input?: unknown; name: string; ref?: string };
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse: { name: string; output?: unknown; ref?: string };
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom: Record<string, any>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
      )[]

    Returns GenerateStreamResponse<TypeOf<O>>

  • Streaming generate calls a generative model based on the provided prompt and configuration. If messages is provided, the generation will include a conversation history in its request. If tools are provided, the generate method will automatically resolve tool calls returned from the model unless returnToolRequests is set to true.

    See GenerateOptions for detailed information about available options.

    const ai = genkit({
    plugins: [googleAI()],
    })

    const { response, stream } = ai.generateStream({
    system: 'talk like a pirate',
    prompt: [
    { media: { url: 'http://....' } },
    { text: 'describe this image' }
    ],
    messages: conversationHistory,
    tools: [ userInfoLookup ],
    model: gemini15Flash,
    });
    for await (const chunk of stream) {
    console.log(chunk.text);
    }
    console.log((await response).text);

    Type Parameters

    • O extends ZodType<any, any, any, O> = ZodTypeAny
    • CustomOptions extends ZodType<any, any, any, CustomOptions> = ZodObject<
          {
              maxOutputTokens: ZodOptional<ZodNumber>;
              stopSequences: ZodOptional<ZodArray<ZodString, "many">>;
              temperature: ZodOptional<ZodNumber>;
              topK: ZodOptional<ZodNumber>;
              topP: ZodOptional<ZodNumber>;
              version: ZodOptional<ZodString>;
          },
          "strip",
          ZodTypeAny,
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
      >

    Parameters

    Returns GenerateStreamResponse<TypeOf<O>>

  • A flow step that executes the provided function. Each run step is recorded separately in the trace.

    ai.defineFlow('hello', async() => {
    await ai.run('step1', async () => {
    // ... step 1
    });
    await ai.run('step2', async () => {
    // ... step 2
    });
    return result;
    })

    Type Parameters

    • T

    Parameters

    • name: string
    • func: () => Promise<T>

    Returns Promise<T>

  • A flow step that executes the provided function. Each run step is recorded separately in the trace.

    ai.defineFlow('hello', async() => {
    await ai.run('step1', async () => {
    // ... step 1
    });
    await ai.run('step2', async () => {
    // ... step 2
    });
    return result;

    Type Parameters

    • T

    Parameters

    • name: string
    • input: any
    • func: (input?: any) => Promise<T>

    Returns Promise<T>