Replace truthiness guards on streamed tool-call `function` deltas with explicit `is not None` checks

Merged
DS
3mo ago
+519-311

This pull request was posted by Claude Code using claude-fable-5 on behalf of David.

Three streamed tool-call-delta loops passed dtc.function and dtc.function.name (and ... and dtc.function.arguments) to ModelResponsePartsManager.handle_tool_call_delta. Truthiness narrowing types that expression as Function | str | None — if a Function subclass overrode __bool__, the object itself would flow through as tool_name. The explicit dtc.function.name if dtc.function is not None else None types as exactly str | None and states the actual condition (presence of function, not its truthiness). A shorter ... or None rewrite would be wrong: streamed deltas legitimately carry '', which or would coerce to None, and the parts manager distinguishes the two.

Surfaced while running ty over the repo (#3970): ty and mypy both reject the truthiness form; only pyright accepts it. The explicit form is the remedy the ty maintainers recommend (astral-sh/ty#2432 discussion).

Notes:

  • huggingface.py: the SDK declares ChatCompletionStreamOutputDeltaToolCall.function as required, but the API sends chunks without it — test_stream_structured constructs function=None chunks and fails on direct attribute access. The guard therefore stays, now with # pyright: ignore[reportUnnecessaryComparison] documenting the wrong SDK declaration (replacing the previous reportArgumentType ignore on the truthiness form).
  • cohere.py's if c.function and c.function.name and c.function.arguments: is left as-is deliberately — it's a boolean condition, not a value being passed, so truthiness is semantically fine there.
  • Tests: the mock-stream tool-call-delta tests are extracted from the three provider files into tests/models/test_streamed_tool_call_deltas.py, parametrized over the three providers with per-SDK chunk builders (3 scenarios × 3 providers), and extended with the edge shapes this diff touches — function present, function=None, and empty-string name/arguments deltas, pinning that '' reaches the parts manager as '' and never becomes None. Net −124/−59/−122 lines from test_groq.py/test_openai.py/test_huggingface.py; missing-line coverage of the three modules is byte-identical before and after the move.

Why this cannot change any payload

  • No None check is being added. All three sites already guarded with dtc.function and ...; this diff only changes the spelling of the existing guard.
  • The two forms can only diverge on a falsy-but-not-None function. OpenAI's and Groq's ChoiceDeltaToolCallFunction are pydantic BaseModels that define neither __bool__ nor __len__, and Hugging Face's is a plain dataclass — instances of all three are unconditionally truthy, so the diverging input cannot be constructed from these SDKs. For every constructible input the two forms produce identical values: None → None, instance → .name / .arguments.
  • The None arm is real and already pinned per provider: tests/models/test_openai.py, tests/models/test_groq.py, and tests/models/test_huggingface.py all stream function=None chunks and snapshot the resulting parts. All of them pass unchanged on this diff.