Skip to content

fix(_transform): guard against IndexError on bare dict annotation - #294

Open
LeonxLJX wants to merge 1 commit into
groq:mainfrom
LeonxLJX:fix/indexerror-bare-dict-annotation
Open

LeonxLJX wants to merge 1 commit into
groq:mainfrom
LeonxLJX:fix/indexerror-bare-dict-annotation

Conversation

@LeonxLJX

Copy link
Copy Markdown

What's broken

Calling transform() or async_transform() with a mapping value typed as a bare dict (no type parameters, e.g. Annotated[dict, PropertyInfo(alias='key')]) raises IndexError. The same crash occurs in the async counterpart _async_transform_recursive.

Reproduction

``python
from typing_extensions import Annotated
from groq._utils._transform import transform, PropertyInfo

transform({"key": {"a": 1}}, Annotated[dict, PropertyInfo(alias="key")])

IndexError: tuple index out of range

``

Root cause

get_args(dict) returns an empty tuple (). Both _transform_recursive (line 183) and _async_transform_recursive (line 349) unconditionally index [1] without checking whether type args exist.

Fix

Added a bounds check in both functions: if get_args(stripped_type) is empty, return data unchanged. When type args are present the existing dict[K, V] path continues to work as before.

Testing

Verified fix works correctly with the reproduction case above. Also ran regression test with Annotated[Dict[str, str], ...] to ensure parameterized dicts still transform properly.

This PR references and supersedes closed PR #275 by devteamaegis.

get_args(dict) returns () but _transform_recursive and _async_transform_recursive
unconditionally indexed [1], causing IndexError on bare dict annotations.

Added bounds check: if get_args(stripped_type) is empty, return data unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant