Implementation:Openai Openai python Runtime Type Inspection
| Knowledge Sources | |
|---|---|
| Domains | SDK_Infrastructure |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for runtime type inspection utilities provided by the openai-python SDK.
Description
The _typing module provides a suite of functions for inspecting and manipulating Python typing constructs at runtime. is_union_type() checks if a type is a Union, is_list_type() and is_sequence_type() detect list/Sequence origins, is_iterable_type() checks for Iterable, is_annotated_type() detects Annotated wrappers, is_required_type() checks for Required markers, and is_typevar() identifies TypeVar instances. is_type_alias_type() supports Python 3.12+ TypeAliasType. The strip_annotated_type() function (LRU-cached with maxsize 8096) recursively unwraps Required[Annotated[T, ...]] to extract the core type T. extract_type_arg() retrieves a type argument by index, and extract_type_var_from_base() resolves generic type variables from inherited base classes, handling both concrete subclasses and generic subclasses.
Usage
Use these utilities when the SDK needs to introspect response types, resolve generic type parameters for deserialization, or strip annotation wrappers during model field processing.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/_utils/_typing.py
- Lines: 1-156
Signature
def is_annotated_type(typ: type) -> bool: ...
def is_list_type(typ: type) -> bool: ...
def is_sequence_type(typ: type) -> bool: ...
def is_iterable_type(typ: type) -> bool: ...
def is_union_type(typ: type) -> bool: ...
def is_required_type(typ: type) -> bool: ...
def is_typevar(typ: type) -> bool: ...
def is_type_alias_type(tp: Any, /) -> TypeIs[TypeAliasType]: ...
@lru_cache(maxsize=8096)
def strip_annotated_type(typ: type) -> type: ...
def extract_type_arg(typ: type, index: int) -> type: ...
def extract_type_var_from_base(
typ: type,
*,
generic_bases: tuple[type, ...],
index: int,
failure_message: str | None = None,
) -> type: ...
Import
from openai._utils._typing import (
is_union_type,
is_list_type,
extract_type_arg,
strip_annotated_type,
extract_type_var_from_base,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| typ | type | Yes | The type to inspect or unwrap |
| index | int | Yes (for extract functions) | The index of the type argument to extract |
| generic_bases | tuple[type, ...] | Yes (for extract_type_var_from_base) | The generic base classes to search |
| failure_message | str or None | No | Custom error message if type resolution fails |
Outputs
| Name | Type | Description |
|---|---|---|
| is_* results | bool | Whether the type matches the predicate |
| strip_annotated_type result | type | The unwrapped core type with Annotated/Required stripped |
| extract_type_arg result | type | The type argument at the given index |
| extract_type_var_from_base result | type | The resolved type variable from the base class hierarchy |
Usage Examples
Basic Usage
from typing import Union, List, Annotated
from openai._utils._typing import (
is_union_type,
is_list_type,
strip_annotated_type,
extract_type_arg,
)
# Check type predicates
is_union_type(Union[str, int]) # True
is_list_type(List[str]) # True
# Strip annotation wrappers
strip_annotated_type(Annotated[str, "metadata"]) # str
# Extract type arguments
extract_type_arg(List[str], 0) # str