Skip to content

Commit b1b8bdc

Browse files
pgrayyjsamuel1
authored andcommitted
models - content - documents (strands-agents#138)
1 parent d43801e commit b1b8bdc

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/strands/types/models/openai.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import abc
10+
import base64
1011
import json
1112
import logging
1213
import mimetypes
@@ -40,6 +41,17 @@ def format_request_message_content(content: ContentBlock) -> dict[str, Any]:
4041
Returns:
4142
OpenAI compatible content block.
4243
"""
44+
if "document" in content:
45+
mime_type = mimetypes.types_map.get(f".{content['document']['format']}", "application/octet-stream")
46+
file_data = base64.b64encode(content["document"]["source"]["bytes"]).decode("utf-8")
47+
return {
48+
"file": {
49+
"file_data": f"data:{mime_type};base64,{file_data}",
50+
"filename": content["document"]["name"],
51+
},
52+
"type": "file",
53+
}
54+
4355
if "image" in content:
4456
mime_type = mimetypes.types_map.get(f".{content['image']['format']}", "application/octet-stream")
4557
image_data = content["image"]["source"]["bytes"].decode("utf-8")

tests/strands/types/models/test_openai.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,24 @@ def system_prompt():
6262
@pytest.mark.parametrize(
6363
"content, exp_result",
6464
[
65-
# Case 1: Image
65+
# Document
66+
(
67+
{
68+
"document": {
69+
"format": "pdf",
70+
"name": "test doc",
71+
"source": {"bytes": b"document"},
72+
},
73+
},
74+
{
75+
"file": {
76+
"file_data": "data:application/pdf;base64,ZG9jdW1lbnQ=",
77+
"filename": "test doc",
78+
},
79+
"type": "file",
80+
},
81+
),
82+
# Image
6683
(
6784
{
6885
"image": {
@@ -79,12 +96,12 @@ def system_prompt():
7996
"type": "image_url",
8097
},
8198
),
82-
# Case 2: Text
99+
# Text
83100
(
84101
{"text": "hello"},
85102
{"type": "text", "text": "hello"},
86103
),
87-
# Case 3: Other
104+
# Other
88105
(
89106
{"other": {"a": 1}},
90107
{

0 commit comments

Comments
 (0)