Skip to content

Files\Image::fromPath attachment causes 400 error when using OpenAI provider #417

@Giarry156

Description

@Giarry156

Description

When using Files\Image::fromPath as an attachment with the OpenAI provider,
the API returns a 400 error:

HTTP request returned status code 400:
Invalid 'input[1].content[1].image_url'. Expected a base64-encoded data URL with an image

The same code works correctly with the Gemini provider.

Steps to reproduce

  1. Create an agent with the OpenAI provider
  2. Prompt the agent passing an image attachment via Files\Image::fromPath
$response = (new MyAgent)->prompt(
    'Describe this image.',
    attachments: [
        Files\Image::fromPath('/path/to/image.jpg'),
    ]
);

Expected behavior

The image is passed correctly to the OpenAI API as a base64-encoded data URL
(data:image/jpeg;base64,...).

Actual behavior

OpenAI rejects the request with a 400 error, suggesting the image is not being
serialized in the format expected by the OpenAI vision API.

Workaround

Bypassing the SDK and calling the OpenAI client directly:

$base64   = base64_encode(file_get_contents($imagePath));
$mimeType = mime_content_type($imagePath);

OpenAI::chat()->create([
    'model' => 'gpt-4o-mini',
    'messages' => [
        [
            'role'    => 'user',
            'content' => [
                ['type' => 'text', 'text' => 'Describe this image.'],
                ['type' => 'image_url', 'image_url' => ['url' => "data:{$mimeType};base64,{$base64}"]],
            ],
        ],
    ],
]);

Environment

  • Laravel AI SDK version: v0.5.1
  • Provider: OpenAI (gpt-4o-mini)
  • PHP version: 8.x
  • Laravel version: 12.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions