Skip to content

Conversation

yasteven
Copy link

@yasteven yasteven commented Sep 17, 2025

PR: Add Mistral AI integration via mistralai

This PR introduces full support for Mistral AI models in Outlines through the official
mistralai SDK. It provides a thin wrapper around mistralai.Mistral, handling prompt formatting, structured outputs, and streaming.

Key Features

  • Wrappers: Mistral wrapper and MistralTypeAdapter for prompt formatting and structured outputs (Pydantic, JSON Schema, Literals).
  • Generation: Support for synchronous + asynchronous text generation, structured generation, and streaming.
  • Structured Outputs: Pydantic/dataclass/JSON schema conversions are supported automatically
  • Model Coverage: Integration works across open and commercial Mistral models (text, multimodal, reasoning), with checks for schema compatibility.
  • Multimodal: Supports Image inputs for Pixtral and other compatible endpoints.
  • Chat Support: Multi-turn conversations with Chat objects (system, user, assistant roles).
  • Custom Inference: Full control over parameters like temperature, max_tokens, top_p, and n.
  • Streaming: Iterative generation for both plain text and structured outputs.
  • Error Handling: Raises RuntimeError, TypeError, and ValueError for common failure cases.

Tests

  • 97 tests total, covering input validation, output formatting, text/structured generation, streaming, and multimodal cases.
  • Includes integration tests against real API calls (nested Pydantic models, structured schemas, image recognition, everything in the documentation).
    Documentation
  • Added mistral.md with:
    • Installation and setup
    • Model overview (open + commercial endpoints as of Sept 2025)
    • Sync/async examples
    • Structured output usage (Pydantic, Literals, nested models)
    • Streaming examples (text + structured)
    • Multimodal (Pixtral image inputs)
    • Chat examples
    • Limitations
    • Troubleshooting

Demos

  • Adapted the dottxt-ai/earnings-reports demo to run with Mistral integration.
  • Extracts financial data (FinancialData, FinancialRow) from 10-K filings successfully.
  • Fixed broken demo code paths (note: GPU-only portions untested locally).

✅ I believe this feature branch is now in good shape for merging into main. Feedback and review are welcome!
— SEE

Copy link
Contributor

@RobinPicard RobinPicard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening a PR @yasteven! There are a few things to change, in particular to ensure consistency with the other models.

@yasteven
Copy link
Author

yasteven commented Sep 19, 2025

Robin,

In order to further ensure consistency with other models, I have added support for async, completed streaming functionality, fixed multi-modal inputs (allowing for image processing), and addressed the issues you identified for the prior PR commits above.

I have also nearly doubled the number of tests to account for these updates, including adding a real image processing test; all of them are passing (though occasionally the mistral image model makes a mistake, thinking 🟥 is the same as 🐈 )

I also updated the mistral.md to reflect the changes, and ran pycodestyle to catch any formatting issues that the pre-commit checks miss.

This is a first-draft, I believe there are issues in the async code as it is presented here which are not captured by the tests (generate_stream needs response format), so I will still need to address that soon. There may still be lingering comments around to eliminate, and there is always room for improvement (especially in the tests and handling mistral SDK errors); but this is now very close to being the same quality as the other models.

Enjoy!

R/ Steven E

@yasteven
Copy link
Author

yasteven commented Sep 19, 2025

I identified and fixed a bug where the new token streaming async implementation for the mistral model was not forwarding the structured output constraints, and since this bug wasn't captured by the tests I created a test for coverage over it. I switched from Mistrals' pixtral model to its small 3.1 for better image test reliability.

The rest of the effort for this pull request on my end is:

  • Get up to date information on Mistral into the documentation ᵉ (e.g, for available and suggested models)
  • remove any superfluous comments

Steven E

ᵉ - I may make a demo of outlines in the future where it retrieves the model list with capabilities enumerated, and generates that section of the documentation.

@yasteven
Copy link
Author

@RobinPicard , I believe I’ve successfully completed the initial effort to make this feature branch ready for merging into main. Please review when you have a chance.

@RobinPicard
Copy link
Contributor

It looks like you forgot some of the comments that have been set as resolved without having been treated @yasteven. Could you also reorganize the commits and force push please?

@yasteven
Copy link
Author

My apologies Robin, I thought the API based models were supposed to emulate native enum support when I reviewed other implementations. I believe my most recent commit addresses those lingering issues, and I pulled in this comment thread and the diff of daf8fdb and 3e4a7dd into some LLMs who inform me that the mistral.py file issues you have raised are in fact resolved. My most recent commit also reduces the tests to a core set of integration tests. I have to move onto another task at the moment so I'll force push in the next day.

@yasteven yasteven force-pushed the feature/mistral-ai-integration branch from daf8fdb to 0de7238 Compare September 27, 2025 01:47
@yasteven
Copy link
Author

@RobinPicard I squashed it down into 0de7238

I don’t have push access to your repository, so I pushed the cleaned-up branch to my fork. The PR is now updated with a single commit on top of main.

I have found this project useful, I have replaced the resume I have been using, with a tool I wrote in a day which takes my personal biography and a job description and generates a tailored resume and cover letter, using outlines for some of the workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants