补全 function_response 中缺失的 name 字段。 按顺序收集所有 function_call 的 name,再按顺序填给缺 name 的 function_response。
(contents: list)
| 340 | |
| 341 | |
| 342 | def _fix_function_response_names(contents: list) -> list: |
| 343 | """ |
| 344 | 补全 function_response 中缺失的 name 字段。 |
| 345 | 按顺序收集所有 function_call 的 name,再按顺序填给缺 name 的 function_response。 |
| 346 | """ |
| 347 | # 第一遍:收集所有 function_call name(按出现顺序) |
| 348 | fc_names = [] |
| 349 | for msg in contents: |
| 350 | if not isinstance(msg, dict): |
| 351 | continue |
| 352 | for part in msg.get("parts", []): |
| 353 | if not isinstance(part, dict): |
| 354 | continue |
| 355 | fc = part.get("functionCall") or part.get("function_call") |
| 356 | if isinstance(fc, dict) and fc.get("name"): |
| 357 | fc_names.append(fc["name"]) |
| 358 | |
| 359 | # 第二遍:给缺 name 的 function_response 按顺序补名 |
| 360 | name_iter = iter(fc_names) |
| 361 | for msg in contents: |
| 362 | if not isinstance(msg, dict): |
| 363 | continue |
| 364 | for part in msg.get("parts", []): |
| 365 | if not isinstance(part, dict): |
| 366 | continue |
| 367 | fr = part.get("functionResponse") or part.get("function_response") |
| 368 | if isinstance(fr, dict) and not fr.get("name"): |
| 369 | fr["name"] = next(name_iter, "unknown") |
| 370 | return contents |
| 371 | |
| 372 | |
| 373 | def _build_variables(model: str, gemini_payload: Dict[str, Any]) -> Dict[str, Any]: |