Skip to content

Add local_file example to align with TS SDK repo #1514

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

Merged
merged 1 commit into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
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
45 changes: 45 additions & 0 deletions examples/basic/local_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import asyncio
import base64
import os

from agents import Agent, Runner

FILEPATH = os.path.join(os.path.dirname(__file__), "media/partial_o3-and-o4-mini-system-card.pdf")


def file_to_base64(file_path: str) -> str:
with open(file_path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")


async def main():
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant.",
)

b64_file = file_to_base64(FILEPATH)
result = await Runner.run(
agent,
[
{
"role": "user",
"content": [
{
"type": "input_file",
"file_data": f"data:application/pdf;base64,{b64_file}",
"filename": "partial_o3-and-o4-mini-system-card.pdf",
}
],
},
{
"role": "user",
"content": "What is the first sentence of the introduction?",
},
],
)
print(result.final_output)


if __name__ == "__main__":
asyncio.run(main())
Binary file not shown.