go-mistral
Unofficial Go client for the Mistral AI REST API, generated with oapi-codegen. `mistral.NewClientWithResponses("https://api.mistral.ai", opts...)` from `github.com/psyb0t/go-mistral` returns a `*mistral.ClientWithResponses` with one typed method per endpoint, named after the spec's operation IDs (`ChatCompletionV1ChatCompletionsPostWithResponse`, `ListModelsV1ModelsGetWithResponse`, `EmbeddingsV1EmbeddingsPostWithResponse` and more). Auth is a bearer API key set through `mistral.WithRequestEdito
Documentation & Preview
go-mistral: setup and reference
Requirements
- Go 1.26.11 or newer, per the module's
go.mod. - Runtime dependency:
github.com/oapi-codegen/runtime. - A Mistral AI API key, created in the API keys section of La Plateforme.
Install
go get github.com/psyb0t/go-mistralThe package name is mistral:
import "github.com/psyb0t/go-mistral"Auth
Mistral AI takes Authorization: Bearer <api key>. Add the header with a RequestEditorFn: for every request through mistral.WithRequestEditorFn, or for one call by passing it as the last argument of that method.
Client options
| Option | Effect | |---|---| | mistral.WithRequestEditorFn(fn) | Runs fn on every request, used for the auth header. | | mistral.WithHTTPClient(doer) | Replaces http.DefaultClient, for timeouts or transport settings. | | mistral.WithBaseURL(url) | Overrides the server passed to the constructor. |
Base URL: https://api.mistral.ai. Paths in the spec already start with /v1.
Calling operations
Method names come from the spec's operation IDs. Each operation X has:
client.XWithResponse(ctx, pathParams..., params, ...)returning*XResponse.- For operations with a JSON body,
client.XWithResponse(ctx, body)takes the typedXJSONRequestBody, andclient.XWithBodyWithResponse(ctx, contentType, io.Reader)takes raw bytes.
Some operations:
| Method | Request | |---|---| | ListModelsV1ModelsGetWithResponse(ctx, *ListModelsV1ModelsGetParams) | List models. | | ChatCompletionV1ChatCompletionsPostWithResponse(ctx, ChatCompletionRequest) | Chat completion. | | EmbeddingsV1EmbeddingsPostWithResponse(ctx, EmbeddingsV1EmbeddingsPostJSONRequestBody) | Embeddings. |
Run go doc github.com/psyb0t/go-mistral ClientWithResponsesInterface to find the rest. Do not guess a method name.
Union types
Fields the spec types as one of several shapes are structs with From<Variant>, As<Variant> and Merge<Variant> methods:
ChatCompletionRequest_Messages_Item:FromUserMessage,FromSystemMessage,FromAssistantMessage,FromToolMessage.UserMessage_Content:FromUserMessageContent0takes a plainstring.AssistantMessage_Content:AsAssistantMessageContent0reads plain text as astring.
Run go doc github.com/psyb0t/go-mistral <UnionType> for the exact variant names of any other union.
Responses
Every XResponse has:
Body []byteandHTTPResponse *http.Response.StatusCode()andStatus().JSON200on success. Many operations also haveJSON422 *HTTPValidationError, whoseDetaillistsValidationErrorvalues withMsg,LocandType.
Other error statuses have no typed field; read Body. A method returns a non-nil error only for transport failures and bodies that do not decode.
Regenerating
client.gen.go is generated. For changes to the client itself, see AGENTS.md in the repository.