Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

lovable.md

339 lines9.4 KB

1---
2title: Lovable AI Gateway
3id: lovable-adapter
4description: "Call Google and OpenAI models with one Lovable project key through Lovable AI Gateway and @tanstack/ai-lovable: chat, images, video, embeddings, speech, and transcription."
5keywords:
6 - tanstack ai
7 - lovable
8 - ai gateway
9 - adapter
10 - chat
11 - responses
12 - embeddings
13 - image generation
14 - video generation
15 - tts
16 - transcription
17---
18​
19The Lovable adapter calls Google and OpenAI models with one project key. It covers chat, images, video, embeddings, speech, and transcription.
20​
21## 1. Install
22​
23<!-- ::start:tabs variant="package-manager" mode="install" -->
24​
25react: @tanstack/ai-lovable
26vue: @tanstack/ai-lovable
27solid: @tanstack/ai-lovable
28svelte: @tanstack/ai-lovable
29preact: @tanstack/ai-lovable
30angular: @tanstack/ai-lovable
31vanilla: @tanstack/ai-lovable
32octane: @tanstack/ai-lovable
33​
34<!-- ::end:tabs -->
35​
36## 2. Set the API key
37​
38Lovable creates a `LOVABLE_API_KEY` for each Cloud project. Put it in the environment of your server:
39​
40```bash
41LOVABLE_API_KEY=your-lovable-key
42```
43​
44`lovableText` and the other short factories read `LOVABLE_API_KEY`. To pass the key in code, use the `create*` factory of the same adapter:
45​
46```typescript
47import { createLovableText } from "@tanstack/ai-lovable";
48​
49const adapter = createLovableText("google/gemini-3.7-flash", process.env.LOVABLE_API_KEY!);
50```
51​
52## 3. Stream a chat from the server
53​
54Model ids use the `google/` or `openai/` form:
55​
56```typescript
57import {
58 chat,
59 chatParamsFromRequest,
60 toServerSentEventsResponse,
61} from "@tanstack/ai";
62import { lovableText } from "@tanstack/ai-lovable";
63​
64export async function POST(request: Request) {
65 const { messages, threadId, runId } = await chatParamsFromRequest(request);
66​
67 const stream = chat({
68 adapter: lovableText("google/gemini-3.7-flash"),
69 messages,
70 threadId,
71 runId,
72 });
73​
74 return toServerSentEventsResponse(stream);
76```
77​
78## 4. Render the chat
79​
80The client is the same for every provider. Point `useChat` at your route:
81​
82```tsx
83import { useState } from "react";
84import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
85​
86export function Chat() {
87 const [input, setInput] = useState("");
88 const { messages, sendMessage } = useChat({
89 connection: fetchServerSentEvents("/api/chat"),
90 threadId: "support-chat",
91 });
92​
93 return (
94 <form
95 onSubmit={(event) => {
96 event.preventDefault();
97 sendMessage(input);
98 setInput("");
99 }}
100 >
101 {messages.map((message) => (
102 <p key={message.id}>
103 {message.parts
104 .map((part) => (part.type === "text" ? part.content : ""))
105 .join("")}
106 </p>
107 ))}
108 <input value={input} onChange={(event) => setInput(event.target.value)} />
109 </form>
110 );
112```
113​
114For Vue, Solid, Svelte, Angular, and the other frameworks, see [Quick Start](../getting-started/quick-start#3-render-the-chat).
115​
116You now have a streaming chat on Lovable. To use an OpenAI model, change the id, for example to `openai/gpt-5.6-sol`.
117​
118## Images
119​
120Make a new image from a prompt:
121​
122```typescript
123import { generateImage } from "@tanstack/ai";
124import { lovableImage } from "@tanstack/ai-lovable";
125​
126const result = await generateImage({
127 adapter: lovableImage("openai/gpt-image-2"),
128 prompt: "a red guitar on a wooden bench",
129 threadId: "thread-1",
130});
131```
132​
133To edit an image, pass image parts. OpenAI image models also take a mask part (`metadata.role === "mask"`). See [Image Generation](../media/image-generation).
134​
135## Video
136​
137> [!NOTE]
138> Video generation is an [experimental feature](../media/video-generation).
139​
140Video runs as a job. `generateVideo()` starts the job and returns a `jobId`. Poll the job until it has an MP4 URL:
141​
142```typescript
143import { generateVideo, getVideoJobStatus } from "@tanstack/ai";
144import { lovableVideo } from "@tanstack/ai-lovable";
145​
146const adapter = lovableVideo("google/veo-3.1-lite");
147​
148const { jobId } = await generateVideo({
149 adapter,
150 prompt: "a red guitar on a wooden bench, slow camera push-in",
151 threadId: "thread-1",
152 duration: "4s",
153 size: "1280x720",
154});
155​
156let status = await getVideoJobStatus({ adapter, jobId });
157while (status.status === "pending" || status.status === "processing") {
158 await new Promise((resolve) => setTimeout(resolve, 5000));
159 status = await getVideoJobStatus({ adapter, jobId });
161​
162console.log(status.status === "completed" ? status.url : status.error);
163```
164​
165- A clip is 4, 6, or 8 seconds. You can write `4`, `"4"`, or `"4s"`. The same forms work for 6 and 8.
166- 1080p and 4K clips are always 8 seconds.
167- 4K works only on `google/veo-3.1-fast` and `google/veo-3.1`.
168- To animate a still frame, pass one image part in `prompt`.
169​
170## Embeddings
171​
172```typescript
173import { embed } from "@tanstack/ai";
174import { lovableEmbedding } from "@tanstack/ai-lovable";
175​
176const result = await embed({
177 adapter: lovableEmbedding("google/gemini-embedding-2"),
178 input: "a red guitar",
179});
180​
181console.log(result.embeddings[0]?.vector);
182```
183​
184## Speech
185​
186```typescript
187import { generateSpeech } from "@tanstack/ai";
188import { lovableSpeech } from "@tanstack/ai-lovable";
189​
190const result = await generateSpeech({
191 adapter: lovableSpeech("openai/gpt-4o-mini-tts"),
192 text: "Welcome to the guitar shop.",
193 threadId: "thread-1",
194 voice: "nova",
195});
196```
197​
198See [Text-to-Speech](../media/text-to-speech).
199​
200## Transcription
201​
202`audio` takes a `File`, a `Blob`, an `ArrayBuffer`, a base64 string, or a data URL:
203​
204```typescript
205import { generateTranscription } from "@tanstack/ai";
206import { lovableTranscription } from "@tanstack/ai-lovable";
207​
208const audio = await fetch("https://example.com/voice-note.mp3").then((response) =>
209 response.blob(),
210);
211​
212const result = await generateTranscription({
213 adapter: lovableTranscription("openai/gpt-4o-mini-transcribe"),
214 audio,
215 language: "en",
216 threadId: "thread-1",
217});
218​
219console.log(result.text);
220```
221​
222See [Transcription](../media/transcription).
223​
224## Summarization
225​
226```typescript
227import { summarize } from "@tanstack/ai";
228import { lovableSummarize } from "@tanstack/ai-lovable";
229​
230const result = await summarize({
231 adapter: lovableSummarize("google/gemini-3.7-flash"),
232 text: "The Fender Stratocaster is a versatile electric guitar.",
233 threadId: "thread-1",
234});
235```
236​
237## Models and options
238​
239### Reasoning
240​
241Set `reasoning` on `chat()`. The types take the levels of each model:
242​
243```typescript
244import { chat } from "@tanstack/ai";
245import { lovableText } from "@tanstack/ai-lovable";
246​
247const stream = chat({
248 adapter: lovableText("openai/gpt-5.6-sol"),
249 messages: [{ role: "user", content: "Plan a database migration." }],
250 threadId: "thread-1",
251 reasoning: "xhigh",
252});
253```
254​
255- Responses (the default) sends `reasoning.effort`. Chat Completions sends `reasoning_effort`.
256- Neither takes `budgetTokens`.
257- The OpenAI and Gemini models take the same levels as on [OpenAI](./openai#reasoning) and [Gemini](./gemini#reasoning).
258​
259### Models
260​
261Pass a model id from the curated lists. TypeScript rejects an id that is not on a list.
262​
263| Activity | Default model | List |
264| --- | --- | --- |
265| Chat | `google/gemini-3.7-flash` | `LOVABLE_CHAT_MODELS` |
266| Images | `openai/gpt-image-2` | `LOVABLE_IMAGE_MODELS` |
267| Video | `google/veo-3.1-lite` | `LOVABLE_VIDEO_MODELS` |
268| Embeddings | `google/gemini-embedding-2` | `LOVABLE_EMBEDDING_MODELS` |
269| Speech | `openai/gpt-4o-mini-tts` | `LOVABLE_TTS_MODELS` |
270| Transcription | `openai/gpt-4o-mini-transcribe` | `LOVABLE_TRANSCRIPTION_MODELS` |
271​
272For chat, `openai/gpt-5.5` is a fast OpenAI choice.
273​
274## Advanced
275​
276### Chat Completions
277​
278The default text adapter uses the OpenAI Responses API. If a model must use Chat Completions, pass `{ api: "chat" }`:
279​
280```typescript
281import { chat } from "@tanstack/ai";
282import { lovableText } from "@tanstack/ai-lovable";
283​
284const stream = chat({
285 adapter: lovableText("openai/gpt-5.5", { api: "chat" }),
286 messages: [{ role: "user", content: "Hello" }],
287 threadId: "thread-1",
288});
289```
290​
291- `api: "responses"` is the same as the default.
292- `api: "chat-completions"` is the same as `api: "chat"`.
293​
294Need a header on each request, or a log of each model call? The chat adapters on this page support [`wrapFetch`](../advanced/middleware#change-the-http-requests-of-a-call).
295​
296### Bring Your Own Key
297​
298Users can paste a Lovable project key in the browser. Import `lovableByok` from `@tanstack/ai-lovable/byok`. The package main entry does not export it.
299​
300```typescript
301import {
302 chat,
303 chatParamsFromRequest,
304 toServerSentEventsResponse,
305} from "@tanstack/ai";
306import { byokMissing, getByokKey } from "@tanstack/ai/byok/server";
307import { createLovableText } from "@tanstack/ai-lovable";
308import { lovableByok } from "@tanstack/ai-lovable/byok";
309​
310export async function POST(request: Request) {
311 const apiKey = getByokKey(request, lovableByok);
312 if (!apiKey) return byokMissing(lovableByok);
313​
314 const { messages, threadId, runId } = await chatParamsFromRequest(request);
315​
316 const stream = chat({
317 adapter: createLovableText("google/gemini-3.7-flash", apiKey),
318 messages,
319 threadId,
320 runId,
321 });
322​
323 return toServerSentEventsResponse(stream);
325```
326​
327See [Bring Your Own Key](../advanced/byok) for the client store and a save UI.
328​
329### Requests
330​
331- Calls go to `https://ai.gateway.lovable.dev/v1`.
332- The adapter sends `Authorization: Bearer`, `Lovable-API-Key`, and `X-Lovable-AIG-SDK: tanstack-ai`.
333​
334### Errors
335​
336- `429 Too Many Requests`: the workspace hit its limit of requests per minute.
337- `402 Payment Required`: the workspace has no credits left.
338​
339See [Lovable AI features](https://docs.lovable.dev/features/ai) for models, credits, and rate limits.