Implementation:Openai Openai node QS Stringify
| Knowledge Sources | |
|---|---|
| Domains | SDK, HTTP_Internals |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The stringify function serializes JavaScript objects into URL-encoded query strings, serving as the SDK's internal query string encoder.
Description
This module is an internal implementation of query string serialization, similar in spirit to the qs npm package. It converts nested JavaScript objects and arrays into URL-encoded key-value pairs. The primary exported function is stringify(object, opts?), which accepts an object and an extensive set of options controlling encoding behavior.
The serialization engine handles nested objects (using bracket or dot notation), arrays (with configurable formats: indices, brackets, repeat, or comma-separated), date serialization via ISO 8601, cyclic reference detection using a WeakMap-based side channel, and custom encoder functions. It supports both UTF-8 and ISO-8859-1 charsets, and can optionally prepend a ? prefix and emit a charset sentinel.
The inner_stringify recursive function does the heavy lifting, walking the object graph while respecting filter functions, sort comparators, and null handling settings. Options are normalized through normalize_stringify_options which validates and applies defaults for all configuration fields, throwing TypeError for invalid inputs.
Usage
This module is used internally by the SDK's HTTP request layer to serialize query parameters before appending them to request URLs. It is not intended for direct use by SDK consumers.
Code Reference
Source Location
- Repository: openai-node
- File: src/internal/qs/stringify.ts
- Lines: 1-385
Signature
export function stringify(object: any, opts?: StringifyOptions): string;
Import
// Internal module - not part of the public API
import { stringify } from 'openai/internal/qs/stringify';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| object | any |
Yes | The object to serialize into a query string |
| opts | StringifyOptions |
No | Configuration options controlling serialization behavior |
Key StringifyOptions fields:
| Name | Type | Default | Description |
|---|---|---|---|
| delimiter | string |
'&' |
Separator between key-value pairs |
| allowDots | boolean |
false |
Use dot notation for nested objects (e.g., a.b=1)
|
| arrayFormat | 'brackets' | 'repeat' | 'comma' | 'indices' |
How arrays are serialized |
| encode | boolean |
true |
Whether to URL-encode keys and values |
| addQueryPrefix | boolean |
false |
Prepend ? to the output
|
| skipNulls | boolean |
false |
Omit keys with null values |
| strictNullHandling | boolean |
false |
Encode null values without = sign
|
| charset | 'iso-8859-1' | 'utf-8' |
Character encoding for percent-encoding |
| filter | Function | None | Filter keys or transform values |
| sort | Function |
None | Comparator to sort keys |
Outputs
| Name | Type | Description |
|---|---|---|
| result | string |
The URL-encoded query string (empty string if input is not an object or is null) |
Usage Examples
Basic Usage
import { stringify } from 'openai/internal/qs/stringify';
stringify({ model: 'gpt-4o', limit: 10 });
// => 'model=gpt-4o&limit=10'
stringify({ a: { b: 'c' } });
// => 'a%5Bb%5D=c' (i.e., a[b]=c)
stringify({ a: { b: 'c' } }, { allowDots: true });
// => 'a.b=c'
stringify({ items: [1, 2, 3] }, { arrayFormat: 'brackets' });
// => 'items%5B%5D=1&items%5B%5D=2&items%5B%5D=3'