Add JS/TS cookbooks for Google Gemini tracing (basic + multimodal)#2878
Add JS/TS cookbooks for Google Gemini tracing (basic + multimodal)#2878JK-Amanda-Goo wants to merge 1 commit intolangfuse:mainfrom
Conversation
Adds two new JS/TS cookbooks for native Google Gemini integration with Langfuse: - js_integration_google_gemini.ipynb: basic text generation tracing using the Langfuse JS SDK, including the correct model name for cost tracking and the flushAsync() pattern required for serverless/Next.js environments. - js_integration_google_gemini_multimodal.ipynb: multimodal (image + text) tracing showing how to capture image token counts from usageMetadata, render images inline in the Langfuse UI, and apply flushAsync() in a Next.js Route Handler. Closes langfuse#2822 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
@JK-Amanda-Goo is attempting to deploy a commit to the langfuse Team on Vercel. A member of the Team first needs to authorize it. |
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
|
| "let fullText = \"\";\n", | ||
| "for await (const chunk of streamResult.stream) {\n", | ||
| " const part = chunk.text();\n", | ||
| " process.stdout.write(part);\n", |
There was a problem hiding this comment.
process is not defined in Deno
process.stdout.write is a Node.js API. The notebook uses the Deno kernel (all other cells use Deno.env), where process is not a global — this line will throw ReferenceError: process is not defined at runtime. Replace with Deno.stdout.write(new TextEncoder().encode(part)).
| " process.stdout.write(part);\n", | |
| Deno.stdout.write(new TextEncoder().encode(part)); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: cookbook/js_integration_google_gemini.ipynb
Line: 192
Comment:
**`process` is not defined in Deno**
`process.stdout.write` is a Node.js API. The notebook uses the Deno kernel (all other cells use `Deno.env`), where `process` is not a global — this line will throw `ReferenceError: process is not defined` at runtime. Replace with `Deno.stdout.write(new TextEncoder().encode(part))`.
```suggestion
Deno.stdout.write(new TextEncoder().encode(part));
```
How can I resolve this? If you propose a fix, please make it concise.| "const genAI = new GoogleGenerativeAI(Deno.env.get(\"GEMINI_API_KEY\") ?? \"\");\n", | ||
| "\n", | ||
| "const MODEL = \"gemini-2.0-flash\";\n", | ||
| "const prompt = \"List three benefits of LLM tracing.\";\n", |
There was a problem hiding this comment.
const re-declarations clash with Step 4 cell in Deno REPL
const genAI, const MODEL, and const prompt are already declared in the Step 4 cell (lines 105–108). Deno's Jupyter kernel shares a single REPL scope across cells, so running this streaming cell after Step 4 will throw SyntaxError: Identifier 'MODEL' has already been declared. Consider renaming these to unique identifiers (e.g. streamGenAI, streamModel, streamPrompt) or removing the duplicate declarations.
Prompt To Fix With AI
This is a comment left during a code review.
Path: cookbook/js_integration_google_gemini.ipynb
Line: 174-177
Comment:
**`const` re-declarations clash with Step 4 cell in Deno REPL**
`const genAI`, `const MODEL`, and `const prompt` are already declared in the Step 4 cell (lines 105–108). Deno's Jupyter kernel shares a single REPL scope across cells, so running this streaming cell after Step 4 will throw `SyntaxError: Identifier 'MODEL' has already been declared`. Consider renaming these to unique identifiers (e.g. `streamGenAI`, `streamModel`, `streamPrompt`) or removing the duplicate declarations.
How can I resolve this? If you propose a fix, please make it concise.
Summary
Adds two JS/TS cookbooks for native Google Gemini tracing with Langfuse (closes #2822). Both examples come from a working Next.js application.
js_integration_google_gemini.ipynb— basic text generation tracing:generateContentcalls withlangfuse.trace()+trace.generation()usageMetadatafields (promptTokenCount,candidatesTokenCount,totalTokenCount) for accurate cost tracking"gemini-2.0-flash") needed to match Langfuse's model registrygenerateContentStream) with token capture after stream completionflushAsync()pattern for serverless/Next.js with a complete Route Handler examplejs_integration_google_gemini_multimodal.ipynb— image + text tracing:inlineDataimage_urlcontent block) so they render in the Langfuse UIpromptTokenCountcovers combined image + text tokensflushAsync()Notes
There is currently no
@arizeai/openinference-instrumentation-google-generative-aipackage for JS (unlike Python), so these cookbooks use the Langfuse JS SDK for manual tracing. Happy to adjust the approach if there is a preferred pattern for manual instrumentation in the JS cookbook collection.Test plan
GEMINI_API_KEYand Langfuse keysflushAsync()examples confirmed working in a Next.js serverless environment🤖 Generated with Claude Code
Disclaimer: Experimental PR review
Greptile Summary
This PR adds two new JS/TS cookbooks demonstrating manual Langfuse tracing for Google Gemini — one for basic text generation (including streaming) and one for multimodal image+text calls. The overall approach (manual
trace/generationwrapping,usageMetadatatoken capture,flushAsync()for serverless) is sound and fills a genuine gap since no auto-instrumentation exists for the Gemini JS SDK.js_integration_google_gemini.ipynbusesprocess.stdout.write, which is a Node.js API unavailable in the Deno kernel — this throwsReferenceErrorat runtime.const genAI,const MODEL, andconst promptin the streaming cell after they are already declared in Step 4, causingSyntaxError: Identifier 'MODEL' has already been declaredwhen cells are run sequentially in Deno's shared REPL scope.Confidence Score: 3/5
Not safe to merge as-is — two P1 runtime errors in the streaming cell will cause the notebook to fail when executed in Deno.
Two P1 bugs in the basic cookbook (wrong stdout API for Deno, const re-declarations) mean the streaming example cannot run as written. The multimodal cookbook is cleaner but has a P2 reliability concern for large images. Both issues are straightforward to fix.
cookbook/js_integration_google_gemini.ipynb — streaming cell needs
process.stdout.writereplaced and duplicate const declarations removed.Important Files Changed
process.stdout.write(Node.js API in a Deno notebook) andconstre-declarations between cells that will throw SyntaxErrors when run sequentially.Sequence Diagram
sequenceDiagram participant App as App Code (JS/TS) participant LF as Langfuse SDK participant Gemini as Google Gemini API participant LFBE as Langfuse Backend App->>LF: langfuse.trace({ name, input }) App->>LF: trace.generation({ name, model, input }) App->>Gemini: model.generateContent(prompt) Gemini-->>App: result (text + usageMetadata) App->>LF: generation.end({ output, usage }) App->>LF: trace.update({ output }) App->>LF: await langfuse.flushAsync() LF->>LFBE: Batch POST events (trace + generation) LFBE-->>LF: 200 OKPrompt To Fix All With AI
Reviews (1): Last reviewed commit: "Add JS/TS cookbooks for Google Gemini tr..." | Re-trigger Greptile