Python: Update hosting agent samples + fixes (#5485)

* Update foundry hosting samples

* Add file data type support

* Fix file content and add more tests

* Fix README

* Address comments

* Fix int tests

* remove temp
This commit is contained in:
Tao Chen
2026-04-28 04:24:05 +00:00
committed by GitHub
parent 9b22ecd119
commit 88347f6494
63 changed files with 1460 additions and 279 deletions
@@ -1507,6 +1507,121 @@ class TestMultiTurnMixedContent:
assert messages[0].contents[1].type == "uri"
assert messages[0].contents[1].uri == "https://example.com/doc.pdf"
async def test_text_and_file_data_input_single_turn(self) -> None:
"""Agent receives a message with text and file content via inline file_data."""
agent = _make_agent(
response=AgentResponse(messages=[Message(role="assistant", contents=[Content.from_text("File received")])])
)
server = _make_server(agent)
resp = await _post_json(
server,
{
"model": "test-model",
"input": [
{
"type": "message",
"role": "user",
"content": [
{"type": "input_text", "text": "Summarize this document"},
{
"type": "input_file",
"file_data": "data:application/pdf;base64,JVBERi0xLjQ=",
"filename": "doc.pdf",
},
],
}
],
"stream": False,
},
)
assert resp.status_code == 200
body = resp.json()
assert body["status"] == "completed"
messages = agent.run.call_args.kwargs["messages"]
assert len(messages) == 1
assert len(messages[0].contents) == 2
assert messages[0].contents[0].type == "text"
assert messages[0].contents[0].text == "Summarize this document"
assert messages[0].contents[1].type == "data"
assert messages[0].contents[1].uri == "data:application/pdf;base64,JVBERi0xLjQ="
async def test_text_mime_file_data_decoded(self) -> None:
"""Agent receives a text/* file_data that is base64-decoded to plain text."""
agent = _make_agent(
response=AgentResponse(messages=[Message(role="assistant", contents=[Content.from_text("Got it")])])
)
server = _make_server(agent)
import base64
encoded = base64.b64encode(b"Hello, world!").decode()
resp = await _post_json(
server,
{
"model": "test-model",
"input": [
{
"type": "message",
"role": "user",
"content": [
{
"type": "input_file",
"file_data": f"data:text/plain;base64,{encoded}",
"filename": "greeting.txt",
},
],
}
],
"stream": False,
},
)
assert resp.status_code == 200
messages = agent.run.call_args.kwargs["messages"]
assert len(messages) == 1
assert messages[0].contents[0].type == "text"
assert messages[0].contents[0].text == "[File: greeting.txt]\nHello, world!"
async def test_text_mime_file_data_invalid_base64_falls_through(self) -> None:
"""Invalid base64 in a text/* file_data falls through to URI passthrough."""
agent = _make_agent(
response=AgentResponse(messages=[Message(role="assistant", contents=[Content.from_text("Got it")])])
)
server = _make_server(agent)
resp = await _post_json(
server,
{
"model": "test-model",
"input": [
{
"type": "message",
"role": "user",
"content": [
{
"type": "input_file",
"file_data": "data:text/plain;base64,!!!invalid!!!",
"filename": "bad.txt",
},
],
}
],
"stream": False,
},
)
assert resp.status_code == 200
messages = agent.run.call_args.kwargs["messages"]
assert len(messages) == 1
assert messages[0].contents[0].type == "data"
assert messages[0].contents[0].uri == "data:text/plain;base64,!!!invalid!!!"
async def test_mixed_text_and_image_input(self) -> None:
"""Agent receives a single message with both text and image content."""
agent = _make_agent(