Simplify `createChatCompletion`
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:
It would be awesome to have stagehand.llmClient.generateText() and stagehand.llmClient.generateObject() like the Vercel AI SDK. Bonus points if you add streamText :)
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?
yeah have at it!
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() }));
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:
stagehand.llmClient.generateText({ prompt, ... })— thin wrapper around the Vercel AI SDK'sgenerateText, pre-wired with the user's configured model and provider so callers don't have to re-supply them.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.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!