Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:Openai Openai node Partial JSON Parser

From Leeroopedia
Knowledge Sources
Domains SDK, Streaming, JSON_Parsing
Last Updated 2026-02-15 12:00 GMT

Overview

The Partial JSON Parser is a vendored utility that parses incomplete or truncated JSON strings, enabling real-time consumption of streamed JSON data before the full payload has been received.

Description

This module provides a custom JSON parser that can handle partial or incomplete JSON input. It is designed for streaming scenarios where JSON data arrives incrementally (e.g., from OpenAI's streaming API responses) and the application needs to extract usable data before the complete JSON document is available.

The parser uses a bitmask-based Allow configuration to control which JSON value types are permitted to be incomplete. The bitmask flags include STR (strings), NUM (numbers), ARR (arrays), OBJ (objects), NULL, BOOL, NAN, INFINITY, and MINUS_INFINITY. By default, all types are allowed to be partial (Allow.ALL). The exported partialParse convenience function allows all types except numbers to be partial (Allow.ALL ^ Allow.NUM), which avoids ambiguity with truncated numeric values.

The internal parsing implementation is a hand-written recursive descent parser that tracks its position through the input string. It handles all standard JSON types (strings, numbers, objects, arrays, booleans, null) plus extended values (NaN, Infinity, -Infinity). When the parser encounters an unexpected end of input, it either returns the partially parsed value (if the type is allowed to be partial) or throws a PartialJSON error. Malformed input that cannot be valid JSON at all raises a MalformedJSON error.

Usage

Use this parser when processing streaming responses from OpenAI where structured JSON is being generated token by token. It is used internally by the SDK's streaming infrastructure to provide incremental parsed results during ChatCompletionStream and similar streaming operations.

Code Reference

Source Location

Signature

function parseJSON(jsonString: string, allowPartial?: number): any;

const partialParse: (input: string) => any;

class PartialJSON extends Error {}
class MalformedJSON extends Error {}

const Allow: {
  STR: number;      // 0b000000001
  NUM: number;      // 0b000000010
  ARR: number;      // 0b000000100
  OBJ: number;      // 0b000001000
  NULL: number;     // 0b000010000
  BOOL: number;     // 0b000100000
  NAN: number;      // 0b001000000
  INFINITY: number; // 0b010000000
  MINUS_INFINITY: number; // 0b100000000
  INF: number;
  SPECIAL: number;
  ATOM: number;
  COLLECTION: number;
  ALL: number;
};

Import

import OpenAI from 'openai';

I/O Contract

Inputs

Name Type Required Description
jsonString string Yes The partial or complete JSON string to parse.
allowPartial number No Bitmask specifying which types may be incomplete. Defaults to Allow.ALL.

Outputs

Name Type Description
result any The parsed JavaScript value. May be a partial object, partial array, truncated string, or any other JSON-representable value depending on how much input was available.

Usage Examples

// The partial JSON parser is used internally by the SDK during streaming.
// Below demonstrates its behavior with incomplete JSON input.

import { partialParse } from 'openai/_vendor/partial-json-parser/parser';

// Complete JSON parses normally
partialParse('{"name": "Alice", "age": 30}');
// => { name: 'Alice', age: 30 }

// Incomplete object: missing closing brace
partialParse('{"name": "Alice", "age": 30');
// => { name: 'Alice', age: 30 }

// Incomplete string value
partialParse('{"name": "Ali');
// => { name: 'Ali' }

// Incomplete array
partialParse('[1, 2, 3');
// => [1, 2, 3]

// Only the opening brace received so far
partialParse('{');
// => {}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment