Problem/Motivation

The Mistral provider currently doesn't support image generation. Mistral offers image generation (powered by Black Forest Labs FLUX1.1 [pro] Ultra) through their Agents & Conversations API using the built-in image_generation tool.
AI module defines a TextToImageInterface for providers to implement

Proposed resolution

Implement TextToImageInterface on MistralProvider.

Mistral's image generation works differently from OpenAI's dedicated /v1/images/generations endpoint — it uses the Agents + Conversations API:

1. Create an Agent with {"type": "image_generation"} in the tools array
2. Start a Conversation with that agent, passing the prompt as input
3. The response contains a tool_file content chunk with a file_id
4. Download the generated image via GET /v1/files/{file_id}/content

The partitech/php-mistral library already provides all the required building blocks (MistralAgentClient, MistralConversationClient, downloadFile()). However, Response::updateFromArray() currently drops tool_file content entries from conversation responses — a small upstream fix is needed to expose the file_id.
So changes:
- Add TextToImageInterface to MistralProvider
- Add 'text_to_image' to getSupportedOperationTypes()
- Add model listing for text_to_image in getConfiguredModels()
- Implement textToImage() using the Agent + Conversation + file download flow
- Return TextToImageOutput with ImageFile containing the downloaded image binary

Remaining tasks

- Upstream fix to partitech/php-mistral: handle tool_file content type in Response::updateFromArray() (or bump minimum version once fixed)
- Implement TextToImageInterface in MistralProvider
- Determine which Mistral models support the image_generation tool and filter in getConfiguredModels()
- Decide on agent lifecycle strategy (create per request vs. cache agent ID)
- Testing

User interface changes

None. The existing AI API Explorer and any modules using text_to_image operation type will automatically pick up Mistral as an available provider.

API changes

MistralProvider will implement TextToImageInterface, adding the textToImage() method.

Data model changes

None

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

petar_basic created an issue. See original summary.

petar_basic’s picture

petar_basic’s picture

Added TextToImageInterface implementation. This uses the Agents API to create a temporary agent with the image_generation tool, starts a conversation with the prompt, then downloads the generated file from the response references.

Note: this depends on a fix in php-mistral (https://github.com/partITech/php-mistral/pull/83) to handle tool_file content in conversation responses. That PR needs to be merged and released before this can land. There's also an upstream bug where MistralConversation properties aren't initialized with defaults — I'll open a separate issue/PR for that.

Also left to do: add some test(s) for the new capability.

petar_basic’s picture

fago’s picture

I reviewed the upstream issue and the MR.

Generally, it looks good, but the mistral-provider class is becoming too big. Could we structure this better, and have a separate helper class/service/trait to call out for?

petar_basic’s picture

Assigned: petar_basic » Unassigned

extracted the image generation logic into a MistralTextToImageTrait, keeping textToImage() on the provider as a thin wrapper. Also merged with latest 1.1.x and aligned error handling to use handleApiException() for proper typed exceptions.

petar_basic’s picture

Status: Needs work » Needs review

Upstream php-mistral fixes (tool_file parsing + uninitialized properties) are merged and released in 0.1.27. Bumped the requirement accordingly.
Also added one test for a successful generation flow
So this is ready for review