Finalize and validate all accumulated tool calls. Returns only valid, complete tool calls as dicts for API compatibility.
(self)
| 185 | return current + new |
| 186 | |
| 187 | def finalize(self) -> list[dict[str, Any]]: |
| 188 | """Finalize and validate all accumulated tool calls. |
| 189 | |
| 190 | Returns only valid, complete tool calls as dicts for API compatibility. |
| 191 | """ |
| 192 | finalized = [] |
| 193 | |
| 194 | for tc in self._accumulated: |
| 195 | # Must have name |
| 196 | if not tc.function.name: |
| 197 | logger.debug(f"Skipping tool call with no name: {tc.id}") |
| 198 | continue |
| 199 | |
| 200 | # Validate arguments JSON |
| 201 | args = tc.function.arguments |
| 202 | if not args or args.strip() == "{}": |
| 203 | args = "{}" |
| 204 | else: |
| 205 | try: |
| 206 | json.loads(args) |
| 207 | except json.JSONDecodeError: |
| 208 | logger.warning( |
| 209 | f"Invalid JSON in tool call arguments, skipping: {args[:100]}" |
| 210 | ) |
| 211 | continue |
| 212 | |
| 213 | # Convert Pydantic model to dict for API |
| 214 | cleaned = tc.to_dict() |
| 215 | finalized.append(cleaned) |
| 216 | |
| 217 | logger.debug(f"Finalized tool call: {tc.function.name}") |
| 218 | |
| 219 | return finalized |
| 220 | |
| 221 | |
| 222 | class StreamingResponseHandler: |