stagehand icon indicating copy to clipboard operation
stagehand copied to clipboard

Simplify `createChatCompletion`

Open kamath opened this issue 1 year ago • 4 comments

Right now, you can theoretically use the stagehand.llmClient object to access the underlying LLM for Stagehand.

This can be helpful for times when you want to ask the LLM custom questions that can guide your next step -- for example, if you're writing a Stagehand script, you might want to ask the LLM for what word to input next. This is possible right now, but it's really cumbersome:

Image

It would be awesome to have stagehand.llmClient.generateText() and stagehand.llmClient.generateObject() like the Vercel AI SDK. Bonus points if you add streamText :)

kamath avatar Apr 13 '25 04:04 kamath

Yeah @kamath, I think we can wrap our new functionality around the vercel ai's generateText() ,generateObject() ,streamText(), streamObject() methods. can i work on this issue?

yash1744 avatar Apr 15 '25 17:04 yash1744

yeah have at it!

kamath avatar Apr 15 '25 23:04 kamath

can we expose commonly used SDK-style utilities directly on the LLMClient, like so : stagehand.llmClient.generateText({ ... }); stagehand.llmClient.generateObject({ ... }); stagehand.llmClient.streamText({ ... });

This mimics the Vercel AI SDK pattern and makes developer usage much more intuitive. These methods are already being imported from ai package so this would just involve exposing them clearly and documenting intending usage.

We could also include a typed wrapper like structuredAsk<T>(prompt: string, schema: ZodType<T>): Promise<T> to support structured JSON output via Zod, e.g.

await stagehand.llmClient.structuredAsk("Return the next UI action", z.object({ action: z.string() }));

Nishant142k2 avatar Aug 08 '25 04:08 Nishant142k2

Hi @kamath — noticed this issue has been sitting open for a while without a PR. If it's still up for grabs, I'd love to pick it up.

I've read through the thread, including @Nishant142k2's suggestion of exposing generateText / generateObject / streamText directly on stagehand.llmClient, which feels right and matches the Vercel AI SDK ergonomics you originally called out.

Proposed scope for the PR:

  1. stagehand.llmClient.generateText({ prompt, ... }) — thin wrapper around the Vercel AI SDK's generateText, pre-wired with the user's configured model and provider so callers don't have to re-supply them.
  2. stagehand.llmClient.generateObject({ prompt, schema, ... }) — same idea for structured Zod-typed output. This unblocks the "ask the LLM what word to input next" use case from the original issue without the cumbersome boilerplate shown in your screenshot.
  3. stagehand.llmClient.streamText({ prompt, ... }) — the bonus, for callers that want token streaming.

Each method would forward the user-supplied options to the underlying AI SDK call (so callers retain full control over temperature, tools, abort signals, etc.) while defaulting model to the LLMClient's configured one.

I'd also add:

  • Unit tests for each method (mocking the AI SDK client).
  • A short docs snippet showing the "ask the LLM mid-script" pattern.
  • An entry in the changeset.

Happy to start as soon as you give the go-ahead. Thanks!

avanish-garg avatar May 15 '26 11:05 avanish-garg