发布

  • [OPIK-7263] [SDK] feat: add Mistral AI Python integration (track_mistral) (#7385)

    frostbyte_neo 发布于 2026-07-08 08:22:21 +00:00

    • [OPIK-7263] [SDK] feat: add Mistral AI Python integration (track_mistral)

    Add a native Opik integration for the Mistral AI Python SDK (mistralai),
    exposed as track_mistral(client). Wraps chat.complete / complete_async and
    chat.stream / stream_async, aggregating streamed chunks into a single span.
    Structured-output calls (chat.parse and variants) delegate to these
    primitives, so they are tracked as a single span named chat_completion_parse
    via the response_format kwarg — no double-logging.

    Captures input messages, output choices, token usage, model, and provider
    ("mistral"), enabling automatic cost tracking. Adds MISTRALAI to the
    LLMProvider enum. Includes an offline test suite (fake_backend), a CI
    workflow wired into the library-integration runner, and a documentation page
    that leads with the native integration and demotes LiteLLM to an alternative.

    Implements OPIK-7263: Mistral AI Python SDK integration.

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

    • fix(mistral): name spans by wrapped method, not response_format

    Naming the span chat_completion_parse whenever response_format was present
    misclassified direct complete(response_format=...) / stream(response_format=...)
    calls as parse. Name spans after the wrapped primitive instead
    (complete -> chat_completion_create, stream -> chat_completion_stream); parse
    delegates to those and shares the name. Also rename the provider-enum test to
    the required test_ convention and fix an "opik" ->
    "Opik" doc typo. Addresses PR #7385 review feedback.

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

    • fix(mistral): pin mistralai to v1 in tests and docs

    CI installed mistralai unpinned and resolved to 2.x, which is a namespace
    package where from mistralai import Mistral no longer works (Mistral moved
    under mistralai.client), breaking test collection. The integration targets the
    mainstream v1 API, so pin the test requirement to mistralai>=1.0.0,<2.0.0 and
    update the docs install command to "mistralai<2".

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

    • refactor(mistral): use only public mistralai API + guard min version
    • Stop importing internal modules (mistralai.utils.eventstreaming,
      mistralai.models.chatcompletionresponse) that move between releases. Use the
      public top-level ChatCompletionResponse, and detect streams by their iterator
      protocol (anext/next) operating on type(stream) rather than the
      private EventStream classes.
    • Add a runtime version guard in track_mistral: raise a clear error when the
      installed mistralai is older than 1.3.0 (the release that introduced the
      EventStream streaming API), read via importlib.metadata. Pin the test
      requirement and docs install to mistralai>=1.3.0,<2.

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

    • feat(llm_usage): add dedicated Mistral usage format

    Add MistralUsage to the llm_usage namespace (parses UsageInfo:
    prompt/completion/total tokens + prompt_tokens_details.cached_tokens) with an
    OpikUsage.from_mistral_dict builder registered under LLMProvider.MISTRALAI.
    The Mistral decorator now parses usage with LLMProvider.MISTRALAI instead of
    piggybacking on the OpenAI completions converter, so it's not coupled to
    OpenAI's format. Adds unit tests for the parser and factory path.

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

    • docs(skills): add dedicated-usage-format guidance to opik-integrations

    Capture the lesson from the Mistral usage work: add a first-class
    llm_usage format per provider instead of reusing another provider's parser.

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

    • fix(mistral): address review nits (dead log arg, import order)
    • Drop the unused str(exception) positional passed to LOGGER.debug with no
      format placeholder in the stream patchers; exc_info=True already captures the
      full exception + traceback.
    • Reorder mistral_usage imports to stdlib-first (typing, then pydantic, then
      local), matching the anthropic/bedrock usage modules.

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

    • fix(mistral): accumulate streamed tool calls by index

    The stream aggregator overwrote message.tool_calls on every chunk that carried
    them, which would drop data if a tool call were split across chunks. Accumulate
    by tool-call index instead, concatenating streamed argument fragments and
    filling in id/name/type as they arrive. Mistral currently emits each tool call
    complete in a single chunk, so this is defensive, but it removes the
    overwrite-and-keep-last behavior. Adds a streaming tool-call test asserting the
    aggregated span output keeps the complete tool call (name + valid-JSON args).

    Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com


    Co-authored-by: Claude Opus 4.8 (1M context) noreply@anthropic.com

    下载附件