Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai python Query String

From Leeroopedia
Revision as of 13:39, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Openai_Openai_python_Query_String.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains SDK_Infrastructure
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for query string serialization provided by the openai-python SDK.

Description

The _qs module provides the Querystring class for serializing Python dictionaries into URL-encoded query strings. It supports configurable ArrayFormat (comma, repeat, indices, brackets) and NestedFormat (dots, brackets) strategies. The stringify() method converts a Params mapping into a URL-encoded string, while stringify_items() returns a list of key-value tuples. The parse() method wraps Python's parse_qs to deserialize query strings back to dictionaries. An internal Options class merges instance defaults with per-call overrides. Module-level convenience functions parse, stringify, and stringify_items are pre-bound to a default Querystring instance (repeat array format, brackets nested format).

Usage

Use this module when the SDK needs to serialize complex query parameters (nested dicts, arrays) into a URL query string for API requests.

Code Reference

Source Location

Signature

ArrayFormat = Literal["comma", "repeat", "indices", "brackets"]
NestedFormat = Literal["dots", "brackets"]

class Querystring:
    array_format: ArrayFormat
    nested_format: NestedFormat

    def __init__(
        self,
        *,
        array_format: ArrayFormat = "repeat",
        nested_format: NestedFormat = "brackets",
    ) -> None: ...

    def parse(self, query: str) -> Mapping[str, object]: ...
    def stringify(self, params: Params, *, array_format=..., nested_format=...) -> str: ...
    def stringify_items(self, params: Params, *, array_format=..., nested_format=...) -> list[tuple[str, str]]: ...

# Module-level convenience
parse = _qs.parse
stringify = _qs.stringify
stringify_items = _qs.stringify_items

Import

from openai._qs import Querystring, stringify, parse

I/O Contract

Inputs

Name Type Required Description
params Mapping[str, Data] Yes Dictionary of query parameters to serialize
query str Yes URL query string to parse (for parse method)
array_format ArrayFormat or NotGiven No How to serialize arrays (default: "repeat")
nested_format NestedFormat or NotGiven No How to serialize nested dicts (default: "brackets")

Outputs

Name Type Description
stringify result str URL-encoded query string
stringify_items result list[tuple[str, str]] List of (key, value) string pairs
parse result Mapping[str, object] Parsed query parameters as a dict

Usage Examples

Basic Usage

from openai._qs import Querystring

qs = Querystring(array_format="comma", nested_format="dots")

# Serialize nested parameters
result = qs.stringify({"filter": {"status": "active"}, "ids": [1, 2, 3]})
# "filter.status=active&ids=1%2C2%2C3"

# Using module-level defaults (repeat + brackets)
from openai._qs import stringify
result = stringify({"tags": ["a", "b"], "page": 1})
# "tags=a&tags=b&page=1"

Related Pages

Page Connections

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