Skip to content

fix: render prompt metadata along with generateoptions #3400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions js/ai/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ export interface GenerateOptions<
context?: ActionContext;
/** Abort signal for the generate request. */
abortSignal?: AbortSignal;
/**
* Additional metadata describing the GenerateOptions, used by tooling. If
* this is an instance of a rendered dotprompt, will contain any prompt
* metadata contained in the original frontmatter.
**/
metadata?: Record<string, any>;
}

export async function toGenerateRequest(
Expand Down
26 changes: 18 additions & 8 deletions js/ai/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ function definePromptAsync<
...resolvedOptions?.config,
...renderOptions?.config,
},
metadata: resolvedOptions.metadata?.metadata
? {
prompt: resolvedOptions.metadata?.metadata,
}
: undefined,
});
// if config is empty and it was not explicitly passed in, we delete it, don't want {}
if (Object.keys(opts.config).length === 0 && !renderOptions?.config) {
Expand Down Expand Up @@ -822,6 +827,18 @@ function loadPrompt(
delete promptMetadata.input.schema.description;
}

const metadata = {
...promptMetadata.metadata,
type: 'prompt',
prompt: {
...promptMetadata,
template: parsedPrompt.template,
},
};
if (promptMetadata.raw?.['metadata']) {
metadata['metadata'] = { ...promptMetadata.raw?.['metadata'] };
}

return {
name: registryDefinitionKey(name, variant ?? undefined, ns),
model: promptMetadata.model,
Expand All @@ -835,14 +852,7 @@ function loadPrompt(
input: {
jsonSchema: promptMetadata.input?.schema,
},
metadata: {
...promptMetadata.metadata,
type: 'prompt',
prompt: {
...promptMetadata,
template: parsedPrompt.template,
},
},
metadata,
maxTurns: promptMetadata.raw?.['maxTurns'],
toolChoice: promptMetadata.raw?.['toolChoice'],
returnToolRequests: promptMetadata.raw?.['returnToolRequests'],
Expand Down
5 changes: 5 additions & 0 deletions js/genkit/tests/prompts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@ describe('prompt', () => {
returnToolRequests: true,
toolChoice: 'required',
tools: ['toolA', 'toolB'],
metadata: {
prompt: {
foo: 'bar',
},
},
});
});

Expand Down