Skip to content

fix(tools): stop CreateChatCompletion from recursing on unsupported response types - #1469

Open
DZH166 wants to merge 1 commit into
FoundationAgents:mainfrom
DZH166:fix/chat-completion-response-type-recursion
Open

DZH166 wants to merge 1 commit into
FoundationAgents:mainfrom
DZH166:fix/chat-completion-response-type-recursion

Conversation

@DZH166

@DZH166 DZH166 commented Nov 10, 2026

Copy link
Copy Markdown

Problem

CreateChatCompletion builds the response schema with two mutually recursive helpers:

def _build_parameters(self):
    if ...: return self._create_type_schema(tp)     # fallback
def _create_type_schema(self, tp):
    if ...: return self._build_parameters()          # fallback

_create_type_schema() only handles list, dict and Union; every other generic alias falls back to _build_parameters(), which falls back again - with no terminating branch. A perfectly normal structured-output type therefore crashes while the tool schema is built in __init__:

typing.Literal['yes','no'] -> RecursionError: maximum recursion depth exceeded
typing.Set[int]            -> RecursionError
typing.Tuple[int, str]     -> RecursionError

Literal is a standard structured-output response type and nothing validates or rejects it (the constructor signature is response_type: Optional[Type] = str).

Fix

For generics that are not list/dict/Union, build the schema from the origin type instead of recursing (Literal[...] → its value type, Set[int]/Tuple[...] → array), so every accepted annotation terminates.

Tests

tests/tools (17 passed): parametrised Literal, Set, Tuple, frozenset all build a schema with response required.

pytest tests/tools -q    # before: 4 failed, 9 passed (new file)   after: 17 passed

…esponse types

_build_parameters fell back to _create_type_schema, which fell back to _build_parameters for every generic alias that is not list/dict/Union, so response_type=Literal[...], Set[int], Tuple[...] or frozenset[str] died with RecursionError while the tool schema was built. Treat other generics as their origin type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant