fix(lib): preserve custom tool calls in parse_chat_completion
What
parse_chat_completion() — the helper behind client.chat.completions.parse() and streaming get_final_completion() — logs a warning and then drops every custom-type tool call:
elif tool_call.type == "custom":
# warn user that custom tool calls are not callable here
log.warning("Custom tool calls are not callable. Ignoring tool call: %s - %s", ...)
# <-- never appended, so the call is discarded
So a supported GPT-5 custom tool call that the model emits disappears from the parsed result — message.tool_calls omits it, and for a custom-only turn it comes back None. The raw (unparsed) ChatCompletion carries the call correctly; only .parse() loses it.
The handling is also internally inconsistent: the trailing else branch already preserves any non-function tool call by appending it unchanged — only custom is special-cased to discard.
Fix
Append the custom tool call instead of dropping it, mirroring the else branch. Custom calls legitimately don't get parsed_arguments (there's no schema to parse their free-form input against), so they're surfaced as-is. The now-inaccurate "Ignoring tool call" warning — which fired on every custom call in normal use — is removed.
Tests
tests/lib/chat/test_parse_custom_tool_calls.py (pure unit tests, no mock server / live API): a custom-only message and a mixed function+custom message. Both fail on next before this change (the custom call is dropped / tool_calls is None) and pass after. The existing tests/lib/chat/test_completions.py suite still passes; ruff and pyright --strict are clean on the changed files.
Scope / honest limitation
ParsedChatCompletionMessage.tool_calls is a generated type narrowed to list[ParsedFunctionToolCall]. With this fix a custom call round-trips through attribute access and its own model_dump(), but its custom payload is still dropped when the entire completion is serialized (pydantic serializes the list by its declared element type) — the same limitation the existing else branch already has for non-function calls. Fully fixing serialization would require widening the generated tool_calls type, which is out of scope for a lib/-only change; happy to coordinate if you'd prefer that path.
Targeted at next to match where external fixes merge.
@seratch would appreciate your eyes on this when you get a chance. small one: parse_chat_completion() drops GPT-5 custom tool calls on a custom-only turn (the raw completion keeps them, only .parse() loses them). the fix just appends the custom call instead of discarding it, mirroring how the else branch already preserves non-function calls. tests included, targeted at next. happy to adjust if you'd rather handle it a different way.
@apcha-oai @jbeckwith-oai small correction on my end: this originally targeted next, which was wrong. 36 of the last 40 merges go to main, and next currently points at the same commit (cbdc98b6). I have retargeted it and rebased onto main, so it is now a single commit, +78/-7 across one source file and one new test file.
The bug: parse_chat_completion logs a warning and then drops every custom-type tool call, so a custom tool call the model made disappears from .parse() output (tool_calls comes back None for a custom-only turn). The else branch immediately below already preserves non-function tool calls unchanged, so custom was the only type being discarded. This appends it the same way.
One thing worth deciding before you spend review time: ParsedChatCompletionMessage.tool_calls is a generated type narrowed to list[ParsedFunctionToolCall], so with this change a custom call survives attribute access and its own model_dump(), but its custom payload is still dropped when the whole completion is serialized. Fixing that properly means widening the generated type, which I cannot do from here.
So: do you want this lib-only fix as-is, or should it be handled upstream in the Stainless spec instead? Happy to close this if it is the latter.
pushed a second commit, and it corrects two things i said earlier on this PR.
the first fix wasn't complete. appending the custom call makes it show up on attribute access, but ParsedChatCompletionMessage.tool_calls was narrowed to list[ParsedFunctionToolCall], and pydantic serializes by the declared type, so dumping a whole parsed completion gave:
[{'id': 'call_custom', 'type': 'custom'}]
custom gone entirely. that isn't just lossy, it doesn't validate back into an equivalent model, so anyone persisting and replaying a parsed completion loses the call. i had written this off in the description as out of scope; it isn't. tool_calls is now list[ParsedChatCompletionMessageToolCallUnion], an annotated discriminated union mirroring ChatCompletionMessageToolCallUnion on the base class with the function member swapped for its parsed subclass, so the annotation matches what the field actually holds and the parsed message stops diverging from the raw one. function calls still come back as ParsedFunctionToolCall with parsed_arguments intact.
i also said pyright was clean on the changed files. it wasn't: the first commit left an error at _completions.py:133 from appending a custom call to a list[ParsedFunctionToolCall]. widening the local accumulator to the same union clears it, so this commit removes an error rather than adding one.
the wider type has two consequences and i handled both here instead of leaving them for callers. the four assert_never sites in the streaming accumulator now have an explicit custom branch — no-ops with comments, since a custom call has no parsed_arguments and the argument-delta/done events are function-tool specific, but explicit so exhaustiveness stays real and a third tool type still trips assert_never. and examples/parsing_tools.py narrows on type before touching .function, which the raw completion has always required.
the tests were the reason i missed this. they dumped each tool call individually, which sidesteps the exact bug. they now assert on the whole-completion dump plus a validate round trip, and all three fail on the previous commit.
verification: ruff check and format clean, pyright clean on all six files, mypy one error below main with no new ones (diffed the full list against the base rather than eyeballing), tests/lib/chat/ 35 passed. remaining tests/lib/ failures are Bedrock/Azure, 31 on both my branch and the base, missing optional deps locally.
@apcha-oai @jbeckwith-oai still happy to split the type change out if you'd rather keep this to lib/, though i think it belongs with the fix — appending the call without widening the annotation only half-preserves it.
@jbeckwith-oai this is a good review and you caught the thing I got wrong. all four of the parse-side asks are in 27e8ae9a, which I pushed before I saw your review, so the overlap is convergent rather than me responding to it — the remaining item, the streaming claim, is in c80482a2.
on the union: tool_calls is now list[ParsedChatCompletionMessageToolCallUnion], an annotated discriminated union on type mirroring ChatCompletionMessageToolCallUnion on the base class with the function member swapped for its parsed subclass. local accumulator in _completions.py widened to the same alias, which is what clears the pyright error at :133. mypy now reports one error fewer than main rather than one more.
on source of truth: parsed_chat_completion.py is it. Parsed* are SDK-only types, not spec types — api.md has zero Parsed entries and there is no OpenAPI shape behind them, so there is nowhere else to put it. CONTRIBUTING says modifications to generated code are persisted through regeneration, so the definition lives with the other hand-maintained overrides in that file, next to the existing # type: ignore[assignment] and the reportIncompatibleVariableOverride suppression that are already there for the same reason. if the Stainless config needs a matching entry to survive a regen, that is on your side and I can't see it from here — worth confirming.
on serialization coverage: the tests now dump the whole parsed completion, plus a ChatCompletion.model_validate(parsed.model_dump()) round trip. you were right that dumping each element under a cast to Any was the tests dodging the public path; that is exactly why I missed this in the first place.
on streaming, you are right and my description was wrong. verified it rather than taking it on faith:
Input should be 'function' [type=literal_error, input_value='custom']
the chunk never validates, so get_final_completion() cannot reach the parser at all. I took the scoping option, not the extension. extending it means widening the generated delta types and the accumulator, and the part I genuinely cannot settle from outside is the wire shape the API emits for a streamed custom call — whether it arrives as type: "custom" deltas with incremental custom.input, or some other shape. guessing at that in generated types seemed worse than scoping. so the PR and its claims are now non-streaming .parse() only, and rather than leave that in prose I pinned it: test_streaming_deltas_cannot_carry_a_custom_tool_call_yet asserts the validation error, so whenever those types gain a custom member the test fails and points straight at the streaming half. the branch comment in _completions.py says the same thing.
happy to do the streaming half as a follow-up if you can tell me the delta shape to target.
state: ruff check and format clean, pyright clean on all six files, mypy 37 vs 38 on base with no new errors (diffed the full list, not eyeballed), tests/lib/chat/ 36 passed. the four new tests fail without the corresponding changes.
@jbeckwith-oai one more data point on the streaming question, since "extend the generated streaming delta union" was the other option you offered and I want to close it out properly rather than leave it as my judgement call.
the Chat Completions chunk schema in the public spec has no custom member at all. openai/openai-openapi @ dc708bb, spec version 2.3.0, ChatCompletionMessageToolCallChunk at openapi.yaml:30700:
type:
type: string
enum:
- function
description: The type of the tool. Currently, only `function` is supported.
x-stainless-const: true
so ChoiceDeltaToolCall.type being Literal["function"] is not an SDK oversight, it is a faithful projection of the spec, and x-stainless-const means the generator will keep emitting it that way. widening it in the SDK would put the types ahead of the documented wire format and would be reverted on the next regeneration.
for contrast, streamed custom tool input does exist in the spec, just on the Responses side: response.custom_tool_call_input.delta (openapi.yaml:57632 and 79061). so the delta shape I was missing is defined for Responses and simply absent for Chat Completions.
which means the streaming half isn't a follow-up I can write against anything today: it needs the chunk schema to gain a custom member upstream first. that makes scoping the correct call rather than the convenient one, and test_streaming_deltas_cannot_carry_a_custom_tool_call_yet is the tripwire for when that changes.
everything else in your review is in 27e8ae9a, pushed before I saw it: the discriminated union on ParsedChatCompletionMessage.tool_calls plus the local accumulator, and whole-completion serialization assertions with a model_validate round trip. mypy is one error below main, pyright clean on all six files.
no further changes planned from me on this one unless you want the scope drawn differently.