Skip to content

feat(ai): support batch fetching multiple prompts with getMany (#4353) - #4690

Draft
vaibhavmashal wants to merge 2 commits into
PostHog:mainfrom
vaibhavmashal:feat/prompts-get-many-4353
Draft

feat(ai): support batch fetching multiple prompts with getMany (#4353)#4690
vaibhavmashal wants to merge 2 commits into
PostHog:mainfrom
vaibhavmashal:feat/prompts-get-many-4353

Conversation

@vaibhavmashal

Copy link
Copy Markdown

Summary

Closes #4353

Adds \getMany()\ to @posthog/ai\ \Prompts\ to allow callers to fetch multiple prompts in a single call with concurrent execution, shared client-side caching, and per-item/shared fallback support.

Changes

  • Exported \PromptBatchItem\ type in @posthog/ai/src/types.ts\ supporting both plain string names and descriptor objects with per-item \GetPromptOptions\ (versions, labels, fallbacks).
  • Implemented \Prompts.prototype.getMany(items, options?)\ in @posthog/ai/src/prompts.ts\ returning a keyed \Record<string, PromptResult>.
  • Reuses the existing client-side cache, TTL logic, and fallback mechanisms across all requested items.
  • Added comprehensive unit tests in \packages/ai/tests/prompts.test.ts\ covering batch fetching, per-prompt options, cache hits across batch calls, and error handling.

Verification

  • Unit tests pass: \pnpm --filter @posthog/ai exec jest tests/prompts.test.ts\ (60 passed, 99.1% coverage).

@vaibhavmashal
vaibhavmashal requested a review from a team as a code owner August 29, 2026 18:39
Copilot AI lite review requested due to automatic review settings August 29, 2026 18:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@marandaneto
marandaneto requested a review from a team August 31, 2026 07:06

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated advisory code review.

): Promise<Record<string, PromptResult>> {
const results: Record<string, PromptResult> = {}

await Promise.all(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: getMany still makes one HTTP request per prompt — Promise.all(items.map(... this.get(...))) performs N independent fetches, so this does not deliver the issue's requested single network call. The existing GET /api/environments/{project_id}/llm_prompts/?content=full endpoint is not a substitute: it returns a paginated list of all latest prompts and cannot select exact names, resolve per-item versions or labels, or define partial missing-prompt behavior. A dedicated backend batch API is needed before the SDK can implement this contract; a focused two-prompt test expecting one fetch receives two.

// Same as loader-globals.ts except includes all additional extension loaders
import './all-external-dependencies'
import './lazy-recorder'
import './external-scripts-loader'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: Keep external loading out of no-external bundles — this import, mirrored in module.full.no-external.es.ts, embeds loadExternalDependency into artifacts whose established contract and regression tests require no script loader. After building, the existing entrypoint test fails both Array and Module assertions because these no-external bundles now contain the loader.

Comment thread packages/ai/src/types.ts
/**
* Single prompt item or descriptor for batch fetching in Prompts.getMany()
*/
export type PromptBatchItem = string | ({ name: string } & GetPromptOptions)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: Export the new descriptor type from the package root — PromptBatchItem appears in the public getMany signature, but packages/ai/src/index.ts does not re-export it. The generated declaration therefore causes import type { PromptBatchItem } from '@posthog/ai' to fail with TS2459.

@@ -0,0 +1,5 @@
---
"posthog-js": patch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: Add an @posthog/ai changeset — this changeset only versions posthog-js, so merging the new public getMany API will not version or publish @posthog/ai. changeset status confirms that the PR schedules only the posthog-js patch release.

@marandaneto
marandaneto marked this pull request as draft August 31, 2026 10:41
@marandaneto
marandaneto requested a review from a team August 31, 2026 10:41
@marandaneto

Copy link
Copy Markdown
Member

moving this to draft until everything is addressed
i believe #4690 (comment) is the real blocker since we'd need an API first
the current approach might shoot our APIs and have infra side effects

@Radu-Raicea
Radu-Raicea requested review from jurajmajerik and removed request for a team September 1, 2026 12:52
@vaibhavmashal

Copy link
Copy Markdown
Author

Makes total sense @marandaneto — having dedicated backend batch API endpoint support first will definitely be much cleaner and safer for infra load. Happy to keep this in draft and align the client interface once the backend endpoint is ready.

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.

Prompt management: support fetching multiple prompts in a single call (batch fetch)

3 participants