Skip to content

Tool with file(byte) return value is not working. how to return PDF in tools? #2285

@AlirezaHaghi

Description

@AlirezaHaghi

Question

I'm not sure if this is a bug, so I'm asking it as a question. The problem is that I would like to define some tools to return file data. For simplicity, I provide you with the code below to return a PDF

@agent.tool_plain
def read_resume() -> BinaryContent:
    '''read the resume if the question was relevant to it'''
    with open('resume.pdf', 'rb') as file:
        pdf = file.read()
    return BinaryContent(pdf, 'application/pdf')

The error doesn't make sense:

pydantic_ai.exceptions.ModelHTTPError: status_code: 500, model_name: gpt-4o, body: {'message': 'The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error. (Please include the request ID req_53c620e5c79764921df279353c91eaae in your email.)', 'type': 'server_error', 'param': None, 'code': None}

the problem is that it sends byte as string to LLM.

Also, this approach does not work with the same reason and result (based on the document):

@agent.tool_plain
def read_resume() -> ToolReturn:
    '''read the resume if the question was relevant to it'''
    return ToolReturn(
        return_value=f"this is the resume as a binary content",
        content=[
            BinaryContent(data=open('resume.pdf', 'rb').read(), media_type="application/pdf"),
        ],
    )

This is an standalone file to reproduce the error:

from pydantic_ai import Agent, BinaryContent
from fpdf import FPDF

from dotenv import load_dotenv
import os
import logfire

load_dotenv()
logfire.configure(token=os.getenv("LOGFIRE_KEY", default=''))
logfire.instrument_pydantic_ai()

PDF_NAME = 'alireza.pdf'

agent = Agent(model="gpt-4o", system_prompt="you are the only one has accesss to pdf. read it before answer the question.")


@agent.tool_plain
def read_resume() -> BinaryContent:
    '''read the resume if the question was relevant to it'''
    with open(PDF_NAME, 'rb') as file:
        pdf = file.read()
    return BinaryContent(pdf, 'application/pdf')


def generate_pdf():
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    pdf.cell(200, 10, txt="I am Alireza.", ln=True, align="C")
    pdf.output(PDF_NAME)

if __name__ == '__main__':
    generate_pdf()
    res = agent.run_sync('read the pdf and say my name')
    print(res.output)

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions