Skip to content

feat(py): list an app's prompts with ai.list_prompts() - #139

Open
huangjeff5 wants to merge 3 commits into
mainfrom
feat/py-list-prompts
Open

huangjeff5 wants to merge 3 commits into
mainfrom
feat/py-list-prompts

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

You can now ask a Genkit app which prompts it has. await ai.list_prompts() returns every prompt as the same Prompt handle ai.prompt(name) gives you, so each entry can be called, rendered, or described. Prompt also gets a name and an async describe() that returns the prompt's description and input JSON schema.

class Guest(BaseModel):
    name: str

# 1. Define a prompt and a variant
ai.define_prompt(name='greet', description='Say hello.', input_schema=Guest, prompt='Hello {{name}}')
ai.define_prompt(name='greet', variant='formal', prompt='Good day {{name}}')

# 2. List them
prompts = await ai.list_prompts()
[p.name for p in prompts]
# => ['greet', 'greet.formal']

# 3. Read what a prompt declares
await prompts[0].describe()
# => PromptDescription(name='greet', description='Say hello.', input_schema={...Guest JSON schema...})

# 4. Render or call it like any other prompt handle
await prompts[0].render({'name': 'Ada'})  # builds the request, no model call
await prompts[0]({'name': 'Ada'})         # generates, same as ai.prompt('greet')({'name': 'Ada'})

This is the core piece #96 needs to serve prompts over MCP: prompts/list reads describe(), and prompts/get reads render(). Tools are covered by #92 (ai.list_tools()), so this PR adds prompts only.

Changes

  • Genkit.list_prompts() -> list[Prompt]: one handle per registered prompt. A variant is its own entry (greet.formal), a .prompt file in a subfolder keeps its folder (myns/a), and partials are not listed. Plugins start first, so if a plugin's init() fails this raises instead of returning a list that is missing that plugin's prompts.
  • Prompt.name: the registered name, including variant and folder; None for a prompt defined without name=. Prompt.ref['name'] now reads from it.
  • await Prompt.describe() -> PromptDescription: loads the prompt first if needed, including a .prompt file nothing has run yet. An unknown name raises the same NOT_FOUND GenkitError that calling the prompt raises; an unnamed prompt raises FAILED_PRECONDITION.
  • PromptDescription exported from genkit: frozen, keyword-only dataclass with name, description, input_schema.

Decisions

  • Return Prompt, not raw Action: users already know the Prompt handle from define_prompt() and ai.prompt(). An Action would expose plugin-API types on Genkit and couldn't generate when called. MCP needs only describe() and render(), both on Prompt.
  • Details come from await describe(), not sync properties: a handle from ai.prompt('greet'), or a .prompt file nobody has run, has not loaded its definition yet. A sync p.description would return None for a prompt that does have one. describe() loads first, so every handle for the same prompt gives the same answer.
  • No promised order: the list comes back in registration order, but the docstring doesn't promise it. Sorting by name would become API we can't change later, and the registry is a dict, so the order is already stable from run to run. ai.list_tools() in feat(py): list an app's tools with ai.list_tools() #92 should follow the same rule.

Breaking changes

None.

ai.list_prompts() returns one Prompt handle per registered prompt,
sorted by registered name: the same handle ai.prompt(name, variant=...)
returns, so each entry can be called, rendered, or described. A variant
is its own entry (greet.formal), a .prompt file in a subfolder keeps its
folder (myns/a), and partials are not listed. Plugins start first, so a
plugin whose init() fails makes it raise.

Prompt gains a name property and an async describe() that returns a
PromptDescription(name, description, input_schema). describe() loads the
prompt first, including a .prompt file nothing has run, so every handle
for the same prompt gives the same answer. An unknown name raises the
same NOT_FOUND that calling the prompt raises.

ai.list_tools() is covered by #92; this adds prompts only.
The registry is a dict, so registration order is already stable. Leave ordering unspecified rather than make sorted-by-name part of the API, matching the ask on list_tools() in #92.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant