Genkit JS API reference
    Preparing search index...

    Function mockModel

    • Defines a programmable mock model for tests. Drive each call's response with respond, and inspect what the model was called with via lastRequest / requests / requestCount.

      const model = mockModel(ai, { respond: () => ({ text: 'a summary' }) });
      const out = (await ai.generate({ model, prompt: 'Summarize: ...' })).text;
      assert.match(model.lastRequest!.messages.at(-1)!.content[0].text!, /Summarize/);

      Streaming and tool calls are first-class:

      mockModel(ai, {
      respond: (req, { sendChunk }) => {
      sendChunk('hel');
      sendChunk('lo');
      return { text: 'hello' };
      },
      });

      mockModel(ai, {
      respond: () => ({ toolRequests: [{ name: 'lookup', input: { id: 1 } }] }),
      });

      For structured output, the mock defaults to native constrained generation (supports.constrained: 'all'), so respond sees request.output.schema and no schema blob is injected into the prompt. Override with supports: { constrained: 'none' } to test the simulated path.

      Parameters

      • registry: Registry | HasRegistry

        a Genkit instance (or anything holding a Registry).

      • Optionaloptions: MockModelOptions

        model name, metadata, and the respond callback.

      Returns MockModel