Skip to content

feat: add MoonshotAI provider with Kimi-K2 model support #2211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

zachmayer
Copy link
Contributor

@zachmayer zachmayer commented Jul 15, 2025

This PR adds a new MoonshotAI provider for Kimi models.

Related PRs:

Changes

  • Add MoonshotAIProvider with OpenAI-compatible API
  • Implements OpenAI-style interface with custom base URL (api.moonshot.ai)
  • Supports tool definitions but disables strict tool validation (as per MoonshotAI docs)
  • Add moonshotai:kimi-k2-0711-preview as known model
  • Configure to use OpenAIModel for compatibility
  • Add comprehensive tests for provider functionality
  • Update CLI and model name tests
  • Uses MOONSHOT_API_KEY environment variable

The provider follows the same pattern as other OpenAI-compatible providers like DeepSeek and Grok.

As requested by @DouweM, I've split the original large PR into 3 focused PRs for easier review.

@zachmayer zachmayer marked this pull request as draft July 16, 2025 14:55
@zachmayer zachmayer marked this pull request as ready for review July 16, 2025 15:36
@zachmayer zachmayer changed the title feat: add o3-pro, grok-4, kimi-k2 model names feat: add o3-pro, grok-4, kimi-k2 Jul 16, 2025
@zachmayer
Copy link
Contributor Author

@DouweM @Kludex I think this is ready for review. I'd be happy to split it up into several PRs if you like (or squash the commits!)

@DouweM DouweM self-assigned this Jul 16, 2025
@DouweM
Copy link
Contributor

DouweM commented Jul 16, 2025

@zachmayer Thanks Zach! As you suggested, can you please split this up into separate PRs so the more straightforward ones can be merged more quickly? Pretty commit history is not that important, we'll end up squashing on merge anyway.

Also, from the MoonshotAIProvider, "Please note that the current version of Kimi API does not support the tool_choice=required parameter." makes me think we'll need a new field on OpenAIModelProfile to stop sending that value in this case and prevent errors. They suggest "you can emphasize the use of a certain tool in the prompt to achieve a similar effect" as a workaround, so I wonder if the default "The final response which ends this conversation" description of the output tool that we require to be called is already sufficient.

Maybe we can keep this PR for the MoonshotAIProvider as I already started talking about it :)

- Add MoonshotAIProvider with OpenAI-compatible API
- Implements OpenAI-style interface with custom base URL
- Supports tool definitions but not strict tool validation
- Add moonshotai:kimi-k2-0711-preview as known model
- Configure to use OpenAIModel for compatibility
- Add comprehensive tests for provider functionality
- Update CLI and model name tests
@zachmayer zachmayer force-pushed the feat/add-kimi-grok-o3pro-clean branch from 4c4ebf7 to dcd7d87 Compare July 17, 2025 14:13
@zachmayer zachmayer changed the title feat: add o3-pro, grok-4, kimi-k2 feat: add MoonshotAI provider with Kimi-K2 model support Jul 17, 2025
@zachmayer
Copy link
Contributor Author

Copy link
Contributor

@DouweM DouweM left a comment

Choose a reason for hiding this comment

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

@zachmayer Thank you! In addition to these comments, can you please add some docs?

Copy link
Contributor

hyperlint-ai bot commented Jul 17, 2025

PR Change Summary

Added a new MoonshotAI provider to support Kimi-K2 models, enhancing compatibility with OpenAI's API.

  • Introduced MoonshotAIProvider with OpenAI-compatible API
  • Implemented support for Kimi-K2 model
  • Added comprehensive tests for provider functionality
  • Updated CLI and model name tests

Modified Files

  • docs/models/openai.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

@zachmayer
Copy link
Contributor Author

I added a moonshot example to the open ai docs


# As the MoonshotAI API is OpenAI-compatible, let's assume we also need OpenAIJsonSchemaTransformer,
# unless json_schema_transformer is set explicitly.
# Also, MoonshotAI does not support strict tool definitions
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually the case or was it just a misinterpretation of the tool_choice=required thing? If so, we can drop this from the comment and drop the field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at their docs:

When you set response_format to {"type": "json_object"}, you must explicitly guide the model to output JSON-formatted content in the prompt and specify the exact format of the JSON, otherwise it may result in unexpected outcomes.

My reading of this is that they don't support strict: mode. You can ask it for a json object, but you need to verybally describe the fields you want in the object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh hmmm, this parameter is different than strict for output schemas though. Now I'm not sure.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah look like they don't support the json_schema response_format, just json_object, so we can set supports_json_object_output to True (they're both False by default).

I don't see anything about strict on tool definitions though, so I think we can drop that.

Copy link
Contributor

@DouweM DouweM left a comment

Choose a reason for hiding this comment

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

Can you look at the merge conflict as well please?


# As the MoonshotAI API is OpenAI-compatible, let's assume we also need OpenAIJsonSchemaTransformer,
# unless json_schema_transformer is set explicitly.
# Also, MoonshotAI does not support strict tool definitions
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah look like they don't support the json_schema response_format, just json_object, so we can set supports_json_object_output to True (they're both False by default).

I don't see anything about strict on tool definitions though, so I think we can drop that.

)
from pydantic_ai.providers import Provider

MoonshotModelName = Literal[
Copy link
Contributor

Choose a reason for hiding this comment

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

For consistency with the provider class:

Suggested change
MoonshotModelName = Literal[
MoonshotAIModelName = Literal[

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

Successfully merging this pull request may close these issues.

2 participants