File size: 1,757 Bytes
0157ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Safe metadata summaries for HTTP 422 validation logging (no raw text content)."""

from __future__ import annotations

from typing import Any


def summarize_request_validation_body(
    body: Any,
) -> tuple[list[dict[str, Any]], list[str]]:
    """Return message shape summary and tool name list for debug logs."""
    messages = body.get("messages") if isinstance(body, dict) else None
    message_summary: list[dict[str, Any]] = []
    if isinstance(messages, list):
        for msg in messages:
            if not isinstance(msg, dict):
                message_summary.append({"message_kind": type(msg).__name__})
                continue
            content = msg.get("content")
            item: dict[str, Any] = {
                "role": msg.get("role"),
                "content_kind": type(content).__name__,
            }
            if isinstance(content, list):
                item["block_types"] = [
                    block.get("type", "dict")
                    if isinstance(block, dict)
                    else type(block).__name__
                    for block in content[:12]
                ]
                item["block_keys"] = [
                    sorted(str(key) for key in block)[:12]
                    for block in content[:5]
                    if isinstance(block, dict)
                ]
            elif isinstance(content, str):
                item["content_length"] = len(content)
            message_summary.append(item)

    tool_names: list[str] = []
    if isinstance(body, dict) and isinstance(body.get("tools"), list):
        tool_names = [
            str(tool.get("name", ""))
            for tool in body["tools"]
            if isinstance(tool, dict)
        ]

    return message_summary, tool_names