openai/openai-python · error · RuntimeError
Could not resolve inner type variable at index {index} for {
Error message
Could not resolve inner type variable at index {index} for {typ} What it means
extract_type_var_from_base resolves the type argument filling a TypeVar in a generic base class (used to infer response types from isomorphic/generic aliases). If the concrete type does not carry the metadata needed to resolve the TypeVar at the requested index — e.g. the class is generic in a different order, is a non-generic subclass, or typing internals do not expose __orig_bases__/args — it raises this RuntimeError. It is essentially an internal typing-inference failure, most commonly seen in tests of the util itself or in exotic generic hierarchies passed to type-inference helpers.
Source
Thrown at src/openai/_utils/_typing.py:156
"This should never happen;\n"
f"Does {cls} inherit from one of {generic_bases} ?"
)
extracted = extract_type_arg(target_base_class, index)
if is_typevar(extracted):
# If the extracted type argument is itself a type variable
# then that means the subclass itself is generic, so we have
# to resolve the type argument from the class itself, not
# the base class.
#
# Note: if there is more than 1 type argument, the subclass could
# change the ordering of the type arguments, this is not currently
# supported.
return extract_type_arg(typ, index)
return extracted
raise RuntimeError(failure_message or f"Could not resolve inner type variable at index {index} for {typ}")
View on GitHub (pinned to 9917c6e28e)
Solutions
- Match the TypeVar order of the base class exactly when subclassing generic bases
- Use typing.TypeVar with the same variance/position; avoid reordering type arguments in subclasses
- If you control the helper, use explicit type annotations instead of runtime inference
- Pin or align with the SDK version whose generic layout your code was written against
Example fix
# before class Wrapper(MyBase[B, A]): ... # A/B order swapped vs base # after class Wrapper(MyBase[A, B]): ... # same order as MyBase's declaration
Defensive patterns
Strategy: validation
Validate before calling
from openai._utils import extract_type_var_from_base
try:
extract_type_var_from_base(cls, TypeVar("T"))
except RuntimeError:
use_explicit_annotation = True Try / catch
try:
extract_type_var_from_base(cls, TV, index=0)
except RuntimeError:
typ = explicit_type_map.get(cls, fallback) Prevention
- Keep TypeVar order aligned with base class
- Prefer explicit annotations over runtime inference
- Don't depend on private typing helpers in product code
When it happens
Trigger: Calling extract_type_var_from_base on a class whose generic base either lacks the TypeVar at that index or reorders type arguments in an unsupported way; subclasses that parameterize generics in a different order than declared; passing a plain (non-generic) class.
Common situations: Building custom generic response wrappers around SDK models; refactoring shared generic base classes and changing TypeVar ordering; SDK upgrades that change internal response typing.
Related errors
- Subclasses of HTTP response classes cannot be passed to `cas
- Pydantic models must subclass our base model type, e.g. `fro
- Unsupported type, expected {cast_to} to be a subclass of {Ba
- Subclasses of HTTP response classes cannot be passed to `cas
- Pydantic models must subclass our base model type, e.g. `fro
AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28).
Data as JSON: /api/errors/3d512d3d3911f981.
Report an issue: GitHub.