Agents file injection during an active run #5128
-
|
Hi, This comes more as a question. The idea is quite simple... I want to be able to allow my agent to read a file (using code interpreter - this is an important step) that had been created during the current run. Let's presume we have the following agent definition. (using openai agents) ` agent = Agent( If the DB result is too big then we are not able to return it as a function result because it is > 512kb. Popular solution are:
In some cases non of those solutions are applicable (maybe data set contains unique values - so we cannot paginate), and we do not know what values to filter on, so we need to retrieve the whole data set. An idea would be to upload the dataset to a file, and give that file id back to the agent. The problem is that, because the file id was not given to the agent at initialization time, it will not be able to acces it. In this case an update session or something will be useful. Are there any guidelines on how to deal with this specific problem? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
This is achievable today without rebuilding the agent — the trick is that OpenAI's code interpreter tool takes import io
import json
from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient
from openai import AsyncOpenAI
openai_files = AsyncOpenAI()
client = OpenAIChatClient()
# Hold a reference to the file_ids list that the tool will append to.
ci_file_ids: list[str] = []
code_interpreter = client.get_code_interpreter_tool(file_ids=ci_file_ids)
INLINE_LIMIT = 400_000 # bytes; keep well under the 512KB tool-result cap
async def get_phone_book() -> str:
"""Return the phone book. Large results are uploaded and exposed via code interpreter."""
contacts = await fetch_contacts_from_db()
payload = json.dumps(contacts).encode()
if len(payload) < INLINE_LIMIT:
return payload.decode()
uploaded = await openai_files.files.create(
file=("phone_book.json", io.BytesIO(payload)),
purpose="assistants",
)
ci_file_ids.append(uploaded.id) # <-- visible on the next tool-loop iteration
return (
f"The phone book was too large to return inline. "
f"Uploaded as file_id={uploaded.id}. "
f"Use the code interpreter to load and query that file."
)
agent = Agent(
client=client,
name="PhoneBookAgent",
instructions=(
"Use get_phone_book to retrieve contacts. "
"If it returns a file_id, use the code interpreter to read that file and answer the user's query "
"instead of calling get_phone_book again."
),
tools=[get_phone_book, code_interpreter],
)
await agent.run("Who works in Engineering and earns over 90k?")What's happening under the hood (relevant if you want to verify or extend the pattern):
A couple of practical notes:
If you prefer a more decoupled approach, you can do the same thing from |
Beta Was this translation helpful? Give feedback.
-
|
Let me just start by saying thanks, you made my day. I've been trying to find a solution to this problem for a while. I'll leave here the complete code for whoever else if facing the same issue: |
Beta Was this translation helpful? Give feedback.
This is achievable today without rebuilding the agent — the trick is that OpenAI's code interpreter tool takes
container.file_idsas a list, and within a singleagent.run()the tool-loop passes the same tool dict to the Responses API on each iteration by reference. So your function tool can upload the oversized result to Files and append the newfile_idto that list before returning. The next round-trip in the same run will see the file.