Gemini AI Stream Parser

A reusable function to parse streaming JSON responses from the Google Gemini API.

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greasyfork.org/scripts/538628/1603085/Gemini%20AI%20Stream%20Parser.js

ئاپتورى
hacker09
نەشرى
0.0.1.20250606201111
قۇرۇلغان ۋاقتى
2025-06-06
يېڭىلانغان ۋاقتى
2025-06-06
Size
1.7 KB
ئىجازەتنامىسى
يوق

If you like my work, please consider supporting it!

Cryptos https://cyber-sec0.github.io

https://www.patreon.com/hacker09





This library will always be updated to the latest version

To always have the latest version, use
// @require https://update.greasyfork.org/scripts/538628/Gemini%20AI%20Stream%20Parser.js


Documentation

Overview

The parseGeminiStream library is a specialized solution for handling streaming responses from the Gemini API. It was created to solve challenges with parsing API responses that include:

  • JSON objects split across multiple data chunks
  • Code blocks containing braces {} that break standard JSON parsing
  • Streaming responses that need to be processed incrementally

This library is essential for developers working with the Gemini API in JavaScript environments, especially in userscripts where existing solutions were inadequate.

Key Features

Robust JSON Parsing

Handles incomplete JSON chunks gracefully, reassembling them as more data arrives

Code Block Support

Accurately processes code snippets containing braces without breaking JSON parsing

Stream Processing

Processes data incrementally as it arrives for responsive user experiences

Installation

Include the library in your project:

// ==UserScript==
// @require      https://update.greasyfork.org/scripts/538628/Gemini%20AI%20Stream%20Parser.js
// ==/UserScript==

Usage

Basic Implementation

//You can parse the text response markdown to HTML if you use the marked.js library below
   // @require            https://update.greasyfork.org/scripts/506699/marked.js

// Make API request with responseType: 'stream'
const request = GM.xmlHttpRequest({
  method: "POST",
  url: `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:streamGenerateContent?key=YOUR_API_KEY`,
  responseType: 'stream',
  // ...other parameters...
  
  onloadstart: async function(response) {
    const reader = response.response.getReader();
    
    // Use the parser library
    await window.parseGeminiStream(reader, (item, markdown) => {
      // Process each chunk
      if (item.error) {
        console.error("API Error:", item.error);
      } else {
        // Update UI with incremental response
        document.getElementById("response").innerHTML = window.marked.parse(markdown);
      }
    });
  }
});

Parameters

ParameterTypeDescription
readerReadableStreamDefaultReaderReader from the Fetch API's Response body
onChunkFunctionCallback that receives parsed objects and accumulated markdown


How It Works

1. Stream Reading

Reads binary chunks from the API response stream and decodes them to text.

2. Buffer Management

Accumulates text chunks in a buffer, handling incomplete JSON objects that span multiple chunks.

🔄

Stream Processing

🧩

JSON Parsing

3. JSON Detection

Identifies complete JSON objects within the buffer by:

  • Locating opening braces {
  • Tracking brace balance
  • Ignoring braces within strings/code blocks

4. Incremental Processing

Parses complete JSON objects and passes them to your callback function immediately.

Special Handling for Code Blocks

The library intelligently handles code blocks that contain braces which would normally break JSON parsing:

String Detection

Tracks when the parser is inside a string to ignore braces used in code

if (buffer[closeBrace] === '"' && 
    buffer[closeBrace - 1] !== '\\') {
  inString = !inString;
}

Brace Balancing

Only counts braces outside of strings to maintain accurate JSON structure

if (!inString) {
  balance += (buffer[closeBrace] === '{') - 
             (buffer[closeBrace] === '}');
}

Integration Example

// Make API request
const request = GM.xmlHttpRequest({
  method: "POST",
  url: `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:streamGenerateContent?key=${API_KEY}`,
  responseType: 'stream',
  headers: { "Content-Type": "application/json" },
  data: JSON.stringify({
    contents: [{ parts: [{ text: "Explain quantum computing" }] }]
  }),
  onloadstart: async function(response) {
    const reader = response.response.getReader();
    let fullResponse = "";
    
    await window.parseGeminiStream(reader, (item, markdown) => {
      if (item.error) {
        console.error("API Error:", item.error);
        return;
      }
      
      // Update UI incrementally
      fullResponse = markdown;
      document.getElementById("ai-response").innerHTML = window.marked.parse(fullResponse);
    });
    
    console.log("Full response:", fullResponse);
  }
});


Bug Reports & Contributions

This library was created to solve a specific challenge with Gemini API responses. If you encounter any issues or have suggestions for improvement:

Please report bugs and issues so I can fix or implement necessary features.

Your feedback helps improve the library for everyone!