Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/stirrup/clients/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,22 @@ def content_to_openai(content: Content) -> list[dict[str, Any]] | str:
content: Either a string or list of content blocks.

Returns:
List of content dictionaries in OpenAI format, or the original string
wrapped in a text content block.
List of content dictionaries in OpenAI format; a non-empty string is wrapped
as a single text part. Empty string/list with no parts becomes ``""``

Raises:
NotImplementedError: If an unsupported content block type is encountered.
"""
if isinstance(content, str):
if content == "":
return ""
return [{"type": "text", "text": content}]

out: list[dict[str, Any]] = []
for block in content:
if isinstance(block, str):
if block == "":
continue
out.append({"type": "text", "text": block})
elif isinstance(block, ImageContentBlock):
out.append({"type": "image_url", "image_url": {"url": block.to_base64_url()}})
Expand All @@ -97,6 +101,8 @@ def content_to_openai(content: Content) -> list[dict[str, Any]] | str:
out.append({"type": "file", "file": {"file_data": block.to_base64_url()}})
else:
raise NotImplementedError(f"Unsupported content block: {type(block)}")
if not out:
return ""
return out


Expand Down