Index of every page on this site: llms.txt
All skills
genkit-ai avatar

/developing-genkit-js

@d12c988
by Genkitgenkit-ai/skills40 stars
13
Run it yourself

Provides guidance on developing AI-powered applications using Genkit in Node.js/TypeScript. Use when the user asks about Genkit, AI agents, flows, or tools in JavaScript/TypeScript, or when encountering Genkit errors, validation issues, type errors, or API problems. Don't use for Genkit in Python, Go, or Dart.

Use this Skill: https://skilld.dev/gh/genkit-ai/skills/developing-genkit-js

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesa2ui.md

≈3.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

A2UI (Agent-to-UI) generative UI

The @genkit-ai/a2ui plugin brings

A2UI, a transport-agnostic, JSON-based streaming UI protocol, to Genkit agents.

A2UI builds on the agent client, so it uses the agent APIs: genkit/beta on the server and genkit/beta/client in the browser. Read Agents first if you have not.

An A2UI-enabled agent streams more than prose. It streams interactive UI surfaces (cards, lists, forms, buttons) that a client renders incrementally as the model responds. The entire server-side integration is a single model middleware: add a2ui() to an agent's use array and nothing else changes.


Install

npm i @genkit-ai/a2ui

To render surfaces in the browser you also need a renderer. A2UI ships renderers for @a2ui/lit, @a2ui/react, and @a2ui/angular. The examples here use Lit:

npm i @a2ui/lit @a2ui/web_core @a2ui/markdown-it lit @lit/context

Server: add the a2ui() middleware

Add a2ui() to your agent's use array. That is the whole server-side setup. use auto-registers the middleware, so no plugin entry is required in genkit({ plugins: [...] }).

import { googleAI } from '@genkit-ai/google-genai';
import { a2ui } from '@genkit-ai/a2ui';
import { genkit } from 'genkit/beta';

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

export const uiAgent = ai.defineAgent({
  name: 'uiAgent',
  model: googleAI.model('gemini-flash-latest'),
  system:
    'You help users. Render an A2UI surface whenever a result is clearer ' +
    'shown than told (weather, comparisons, lists, forms). Keep prose brief; ' +
    'put the substance in the UI.',
  use: [a2ui()], // defaults to the bundled 'basic' catalog
});

It works identically on a one-shot ai.generate:

const res = await ai.generate({
  model: googleAI.model('gemini-flash-latest'),
  prompt: 'Show me the weather in Tokyo',
  use: [a2ui()],
});

Serve the agent over HTTP with expressHandler (see Deploying agents); the browser talks to it with remoteAgent. expressHandler reads the request body, so mount express.json() before the route or the first turn fails with request.body is undefined:

import { expressHandler } from '@genkit-ai/express';
import express from 'express';

const app = express();
app.use(express.json()); // required: expressHandler reads req.body
app.post('/api/uiAgent', expressHandler(uiAgent));

Options

Pass options to a2ui({ ... }):

Option Default Description
catalog 'basic' Id of the catalog describing what the agent may render.
instructions 'system' Where to inject catalog capabilities. 'none' injects nothing (supply your own instructions instead).
validate 'warn' Validate emitted envelopes. 'warn' logs and drops bad blocks; 'strict' throws; 'off' skips it.
surfaceId fresh UUID Surface id policy. Defaults to a new UUID per surface; pass a fixed string to reuse one id per surface.
version 'v0.9' Protocol version stamped on envelopes.

Use validate: 'strict' during development to fail fast on malformed JSON or components outside the catalog. See Security for what 'strict' does and does not check.

Client: render surfaces

@genkit-ai/a2ui/client is browser-safe (no Node dependencies). Consume the agent with remoteAgent, pull A2UI envelopes off each chunk with a2uiEnvelopesFromParts, and feed whole envelopes to a renderer. A2UI travels as data parts on the raw model chunk, so read them from chunk.raw.modelChunk?.content.

import { A2uiSurface, basicCatalog } from '@a2ui/lit/v0_9';
import '@a2ui/lit/v0_9'; // registers <a2ui-surface> + basic components
import { MessageProcessor } from '@a2ui/web_core/v0_9';
import { a2uiEnvelopesFromParts } from '@genkit-ai/a2ui/client';
import { remoteAgent } from 'genkit/beta/client';

const chat = remoteAgent({ url: '/api/uiAgent' }).chat();

const processor = new MessageProcessor([basicCatalog]);
processor.onSurfaceCreated((surface) => {
  // `a2ui-surface` is not in the DOM's tag map, so narrow to the renderer's
  // element class for a typed `.surface` property.
  const el = document.createElement('a2ui-surface') as A2uiSurface;
  el.surface = surface;
  document.getElementById('log')!.appendChild(el);
});

const turn = chat.sendStream('What is the weather in Tokyo?');
for await (const chunk of turn.stream) {
  if (chunk.text) appendProse(chunk.text);
  const envelopes = a2uiEnvelopesFromParts(chunk.raw.modelChunk?.content);
  if (envelopes.length) processor.processMessages(envelopes);
}
await turn.response; // surfaces any server error / finalizes the turn

remoteAgent manages the session id for you, so a single chat keeps the whole conversation server-side (the agent's session store holds history).

Lightweight helper (no full agent client)

If you do not want to drive remoteAgent yourself, the client entrypoint also ships streamA2uiAgent, an async generator that yields { type: 'text' } and { type: 'envelopes' } events:

import { streamA2uiAgent } from '@genkit-ai/a2ui/client';

for await (const ev of streamA2uiAgent({ url: '/api/uiAgent', message: 'weather in Tokyo' })) {
  if (ev.type === 'text') appendProse(ev.text);
  else processor.processMessages(ev.envelopes);
}

StreamA2uiAgentOptions also accepts sessionId, headers, and abortSignal.

Handling user actions

When a user interacts with a surface (for example, presses a Button), the renderer's action callback hands you a typed A2uiClientAction. Turn it into an agent input with actionToMessage and send it as the next turn:

import { actionToMessage } from '@genkit-ai/a2ui/client';

const processor = new MessageProcessor([basicCatalog], (action) => {
  const turn = chat.sendStream({ message: actionToMessage(action) });
  // ...consume turn.stream like above...
});

The action's name is sent as the user message; the full action (including its context) is attached as an a2ui data part so the agent can react to it.

Forms

Input components (TextField, CheckBox, Slider) do not send their values automatically. To capture what the user entered, the model must:

  1. Bind each input's value to a data-model path ({ "path": "/email" }).
  2. Echo those same paths in the submit Button's action.event.context.

The catalog capabilities injected into the system prompt already instruct the model to do this. Without both steps, the action arrives with an empty context.

Renderer requirements (Lit basic catalog)

The @a2ui/lit basic catalog needs two host-side pieces to render fully:

  • A MarkdownRenderer provided via Lit context (for example, backed by @a2ui/markdown-it). Text heading variants render as Markdown; without a renderer, headings show as literal ##.
  • The Material Symbols Outlined font. The Icon component renders names as font ligatures; without the font, icon names show as literal text. Load it in your HTML:
    <link rel="stylesheet"
      href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0..1,0" />

Wire up the Markdown renderer and inject the basic catalog styles once at startup:

import { Context } from '@a2ui/lit/v0_9';
import { renderMarkdown } from '@a2ui/markdown-it';
import { injectBasicCatalogStyles } from '@a2ui/web_core/v0_9/basic_catalog';
import { ContextProvider } from '@lit/context';

injectBasicCatalogStyles();
new ContextProvider(document.body as any, {
  context: Context.markdown,
  initialValue: renderMarkdown,
});

Custom catalogs

The catalog option is a catalog id resolved from the Genkit registry. The bundled 'basic' catalog (mirroring @a2ui/web_core's basic catalog) is the default and needs no registration. A catalog describes the components the model may emit:

  • id: globally-unique URI (also used as catalogId on createSurface).
  • components[]: each with name (matches the renderer type), description (one-line summary), and props (a compact, model-facing text description of the component's props, kept as plain text to minimize prompt tokens).

Register a catalog with loadCatalog, then reference it by id. Load from a JSON file:

import { loadCatalog } from '@genkit-ai/a2ui';

await loadCatalog(ai, { id: 'my-catalog', file: './my-catalog.json' });

Or define one in memory (start from basicCatalog and add your own component):

import { a2ui, basicCatalog, loadCatalog, type A2uiCatalog } from '@genkit-ai/a2ui';

const myCatalog: A2uiCatalog = {
  id: 'https://my-app.org/catalogs/weather.json',
  components: [
    ...basicCatalog.components,
    {
      name: 'Gauge',
      description: 'A circular gauge visualizing a single numeric value.',
      props: 'value: number or { path } binding (required); min?: number; max?: number; label?: string; unit?: string.',
    },
  ],
};

await loadCatalog(ai, { id: 'my-catalog', catalog: myCatalog });

export const uiAgent = ai.defineAgent({
  name: 'uiAgent',
  model: googleAI.model('gemini-flash-latest'),
  use: [a2ui({ catalog: 'my-catalog', validate: 'strict' })],
});

The client must register a matching renderer under the same catalog id, and the component name must match on both sides. Otherwise the model emits a component the client cannot render. Catalogs live in the registry under value type a2ui-catalog.

Security and the trust boundary

Generative UI moves model output into the DOM, so treat every surface an agent emits as untrusted input. The validate option (including 'strict') checks envelope structure and component type names against the catalog only. It does not validate component props or data-model values: model-controlled values such as Image.url and Text (inline Markdown that a renderer may turn into HTML) pass through untouched. 'strict' is a well-formedness check, not a security boundary.

  • The renderer/catalog owns prop sanitization. Whatever renders a surface is responsible for escaping and sanitizing prop values before they reach the DOM.
  • Restrict remote sources at the host. Serve the app with a Content Security Policy that limits img-src and other fetch directives to origins you trust.
  • Do not put secrets in the data model. Anything bound into a surface's data model can be echoed back through an action's context.

For server-side control over props (for example, allow-listing image hosts), add your own model middleware after a2ui() to inspect and rewrite the emitted a2ui parts.

How it works

A2UI rides on its own part channel: a Genkit data part with mime type application/a2ui+json whose data is { envelopes: [...] }. On each model call inside the agent's tool loop, a2ui():

  1. Injects the catalog's capabilities into the system prompt (unless instructions: 'none').
  2. Intercepts the model output (streamed chunks and the final message).
  3. Extracts a2ui fenced code blocks from the model's text.
  4. Validates them against the catalog (per validate).
  5. Rewrites them into canonical a2ui data parts.

Inbound a2ui parts (a surface action sent back as the next turn, or replayed history) are summarized into plain text before the underlying model sees them, so a model that does not understand the a2ui mime type can still reason about prior surfaces and user actions.

For a complete, runnable example (Express backend plus a Vite + Lit frontend), see the a2ui testapp in the Genkit JS repo.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts13d5 checks · Risk SAFE
  • Gen Agent Trust Hub14d

    The skill is a comprehensive development guide for the Genkit JS framework. It provides technical instructions, code examples, and troubleshooting steps. All referenced tools and packages are official or standard industry dependencies. No security risks were identified.

  • Socket13d

    No alerts

  • Snyk14d

    Risk: LOW · No issues

  • Runlayer8mo

    6 files scanned · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at d12c988. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 2 weeks ago
metadata
{
  "version": "1.0.0",
  "category": "AiAndMachineLearning"
}
  • TypeScript
  • genkit
  • nodejs
  • ai-agents
  • flows
  • llm
  • google-ai
  • openai
  • anthropic

README badge

README badge for genkit-ai/skills/developing-genkit-js

Develop AI-powered applications using Genkit in Node.js and TypeScript, including flows, tools, and multi-model integrations. Covers Genkit CLI setup, flow definition with schema validation, provider selection (Google AI, OpenAI, Anthropic, Ollama), and error troubleshooting for version 1.29.0 and later.

Generated from the current SKILL.md.

What Genkit CLI version does this skill require?
Minimum CLI version 1.29.0. Verify with `genkit --version` and upgrade with `npm install -g genkit-cli@^1.29.0` if needed.
Does this skill cover pre-1.0 Genkit or older versions?
No. Genkit went through a major breaking API change. This skill assumes v1.x (1.29.0+) and will not reliably handle pre-1.0 code or deprecated APIs.
What AI providers does this skill support?
Genkit is provider-agnostic and supports Google AI, OpenAI, Anthropic, Ollama, and others. The skill defaults to Google AI if no provider is specified.
How should I troubleshoot Genkit errors?
Always read the Common Errors reference first to identify known patterns and their fixes. Use `genkit docs:search` only if the error is not documented there.
Does this skill work with Next.js, Firebase, or Express?
Yes. The skill detects your framework via package.json and adapts the implementation to framework-specific patterns like @genkit-ai/next or @genkit-ai/firebase.

Generated from the current SKILL.md. These answers refresh after source changes.