Skip to content

Optionally force file URLs to be downloaded agent-side before sending them to OpenAI API #3102

@nicolas-chaulet

Description

@nicolas-chaulet

Initial Checks

Description

OpenAI URL handling is very unstable it seems (plenty of issues in their forum with regressions after it was fixed)

https://community.openai.com/t/invalid-image-url-error-code-for-image-urls-without-file-extension/1141898
https://community.openai.com/t/invalid-image-error-in-gpt-4o-when-using-s3-presigned-url/794549
https://community.openai.com/t/invalid-image-error-in-gpt-4-vision/505843/18

Pydatnic AI is sending urls, considering the unstable nature of this feature on the OpenAI side of things would it make sense to move to sending base64 strings instead?

Below is some code to recreate the issue on the openai side and pyndatic side of things (same error)

Example Code

import httpx
from openai import OpenAI
from pydantic_ai import Agent, ImageUrl

client = OpenAI()
url = "https://graph.facebook.com/NASA/picture?type=large"
data = httpx.get(url, follow_redirects=True).content
# assert data

print("Image URL is valid", len(data), "bytes")

try:
    response = client.chat.completions.create(
        model="gpt-5",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {"url": url},
                    },
                ],
            }
        ],
    )
except Exception as e:
    print("Error from OpenAI:", e)


agent = Agent(model="gpt-5")
response = agent.run_sync(
    [
        "What is in this image?",
        ImageUrl(
            url=url,
            media_type="image/png",
        ),
    ]
)

Python, Pydantic AI & LLM client version

pydantic-ai: 1.0.15

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions