Repository navigation
fix(guardrails): walk Responses-API text taxonomy in shared content helpers #32542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
4df1417
0cfaf50
20dc5a3
7e42d65
04bd0fc
ff5823d
a5bfcc4
18d3a6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,11 @@ def is_text_content_call_type(call_type: str) -> bool: | |
| return call_type in TEXT_CONTENT_CALL_TYPES | ||
|
|
||
|
|
||
| TEXT_PART_TYPES: FrozenSet[str] = frozenset({"text", "input_text", "output_text"}) | ||
|
|
||
| _INSPECTION_SAFE_ROLES: FrozenSet[str] = frozenset({"system", "user", "assistant"}) | ||
|
|
||
|
|
||
| def _iter_text_parts_in_content(content: Any) -> Iterator[str]: | ||
| """Yield text fragments from a ``message.content`` value (string or | ||
| multimodal list). Non-text parts (images, audio, …) are skipped.""" | ||
|
|
@@ -48,7 +53,7 @@ def _iter_text_parts_in_content(content: Any) -> Iterator[str]: | |
| continue | ||
| if not isinstance(part, dict): | ||
| continue | ||
| if part.get("type") == "text": | ||
| if part.get("type") in TEXT_PART_TYPES: | ||
| text = part.get("text") | ||
| if isinstance(text, str) and text: | ||
| yield text | ||
|
|
@@ -58,14 +63,20 @@ def _coerce_input_to_messages(input_value: Any) -> List[Dict[str, Any]]: | |
| """Coerce a Responses-API ``data["input"]`` value into chat-style messages.""" | ||
| if isinstance(input_value, str): | ||
| return [{"role": "user", "content": input_value}] | ||
| if isinstance(input_value, list): | ||
| if input_value and all(isinstance(item, dict) and "role" in item for item in input_value): | ||
| return list(input_value) | ||
| # Mixed lists (content-part dicts + bare strings) and pure | ||
| # string/dict lists all become a single user message; the content | ||
| # iterator below handles each element type uniformly. | ||
| return [{"role": "user", "content": input_value}] | ||
| return [] | ||
| if not isinstance(input_value, list): | ||
| return [] | ||
| messages: List[Dict[str, Any]] = [] | ||
|
ryan-crabbe-berri marked this conversation as resolved.
Resolved
|
||
| for item in input_value: | ||
| if isinstance(item, str): | ||
| messages.append({"role": "user", "content": item}) | ||
| elif isinstance(item, dict): | ||
| if item.get("type") in TEXT_PART_TYPES: | ||
| messages.append({"role": "user", "content": [item]}) | ||
| elif "content" in item: | ||
| messages.append({"role": item.get("role") or "user", "content": item["content"]}) | ||
| elif item.get("type") == "function_call_output" and "output" in item: | ||
| messages.append({"role": "user", "content": item["output"]}) | ||
| return messages | ||
|
|
||
|
|
||
| def _iter_inspection_messages(data: Dict[str, Any]) -> Iterator[Dict[str, Any]]: | ||
|
|
@@ -112,7 +123,7 @@ def _rewrite_content(content: Any) -> Any: | |
| new_parts.append(visit(part)) | ||
| elif ( | ||
| isinstance(part, dict) | ||
| and part.get("type") == "text" | ||
| and part.get("type") in TEXT_PART_TYPES | ||
| and isinstance(part.get("text"), str) | ||
| and part["text"] | ||
| ): | ||
|
|
@@ -136,25 +147,20 @@ def _rewrite_content(content: Any) -> Any: | |
| data["input"] = visit(input_value) | ||
| return visited | ||
| if isinstance(input_value, list): | ||
| # List of full messages: rewrite each message's content. | ||
| if input_value and all(isinstance(item, dict) and "role" in item for item in input_value): | ||
| for item in input_value: | ||
| if "content" in item: | ||
| item["content"] = _rewrite_content(item["content"]) | ||
| return visited | ||
| # List of content parts and/or bare strings: rewrite in place. | ||
| for idx, item in enumerate(input_value): | ||
| if isinstance(item, str) and item: | ||
| visited += 1 | ||
| input_value[idx] = visit(item) | ||
| elif ( | ||
| isinstance(item, dict) | ||
| and item.get("type") == "text" | ||
| and isinstance(item.get("text"), str) | ||
| and item["text"] | ||
| ): | ||
| visited += 1 | ||
| input_value[idx] = {**item, "text": visit(item["text"])} | ||
| if isinstance(item, str): | ||
| if item: | ||
| visited += 1 | ||
| input_value[idx] = visit(item) | ||
| elif isinstance(item, dict): | ||
| if item.get("type") in TEXT_PART_TYPES: | ||
| if isinstance(item.get("text"), str) and item["text"]: | ||
| visited += 1 | ||
| input_value[idx] = {**item, "text": visit(item["text"])} | ||
| elif "content" in item: | ||
| item["content"] = _rewrite_content(item["content"]) | ||
| elif item.get("type") == "function_call_output" and "output" in item: | ||
| item["output"] = _rewrite_content(item["output"]) | ||
| return visited | ||
|
|
||
| return visited | ||
|
|
@@ -221,5 +227,7 @@ def build_inspection_messages(data: Dict[str, Any]) -> List[Dict[str, str]]: | |
| if not text: | ||
| continue | ||
| role = message.get("role", "user") or "user" | ||
| if role not in _INSPECTION_SAFE_ROLES: | ||
| role = "user" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so basically if its not a role we recognize then we say its user role instead, do i have that right? can you give an example of this happening? ive never seen this but i could imagine it being the case
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. {"role":"tool","tool_call_id":"c1","content":"sunny"} gets flattened to {"role":"tool","content":"sunny"}, which AIM rejects because tool_call_id is missing, so we coerce it to {"role":"user","content":"sunny"} to keep it valid |
||
| flattened.append({"role": role, "content": text}) | ||
| return flattened | ||
Uh oh!
There was an error while loading. Please reload this page.