| 62 | |
| 63 | |
| 64 | def _extract_response_text(payload: dict[str, Any]) -> str: |
| 65 | output_text = payload.get("output_text") |
| 66 | if isinstance(output_text, str) and output_text: |
| 67 | return output_text |
| 68 | |
| 69 | texts: list[str] = [] |
| 70 | for item in payload.get("output") or []: |
| 71 | if not isinstance(item, dict): |
| 72 | continue |
| 73 | if item.get("type") != "message": |
| 74 | continue |
| 75 | for content in item.get("content") or []: |
| 76 | if not isinstance(content, dict): |
| 77 | continue |
| 78 | if isinstance(content.get("text"), str): |
| 79 | texts.append(content["text"]) |
| 80 | elif isinstance(content.get("output_text"), str): |
| 81 | texts.append(content["output_text"]) |
| 82 | return "\n".join(texts) |
| 83 | |
| 84 | |
| 85 | def _usage_metrics_from_response_payload(payload: dict[str, Any]) -> dict[str, int]: |