-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path_content_utils.py
More file actions
64 lines (52 loc) · 2.01 KB
/
_content_utils.py
File metadata and controls
64 lines (52 loc) · 2.01 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Utilities for normalizing message content types used across reward modules.
`Message.content` may be a `str` or a list of OpenAI-style content parts.
These helpers convert such values into plain strings suitable for text
processing without triggering type checker errors.
"""
from typing import Any, List, Optional, Union
from ..models import ChatCompletionContentPartTextParam
def to_text(
content: Optional[Union[str, List[ChatCompletionContentPartTextParam]]]
) -> str:
"""Return plain text from a Message.content-like value.
- If content is None, returns "".
- If content is a string, returns it unchanged.
- If content is a list of ChatCompletionContentPartTextParam, joins their
text fields with a single space.
"""
if content is None:
return ""
if isinstance(content, str):
return content
# Join any text parts conservatively with a space
try:
return " ".join(part.text for part in content)
except Exception:
# Best-effort fallback if structure is unexpected
return ""
def to_text_any(content: Any) -> str:
"""Best-effort conversion of arbitrary content values to text.
Handles:
- None -> ""
- str -> unchanged
- List[ChatCompletionContentPartTextParam] -> join texts
- List[dict-like] with key 'text' -> join their 'text' values
- Other -> "" (avoids surprising stringification of complex objects)
"""
if content is None:
return ""
if isinstance(content, str):
return content
# List of typed content parts
if isinstance(content, list) and all(
hasattr(p, "text") and isinstance(getattr(p, "text"), str) for p in content
):
return " ".join(getattr(p, "text") for p in content)
# List of dicts with 'text' entries
if isinstance(content, list) and all(isinstance(p, dict) and "text" in p for p in content):
try:
return " ".join(str(p.get("text", "")) for p in content)
except Exception:
return ""
return ""