10000 Add OpenRouter chat model by piiq · Pull Request #448 · jackmpcollins/magentic · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add OpenRouter chat model #448

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

Merged
merged 12 commits into from
Jun 22, 2025
Merged

Conversation

piiq
Copy link
Contributor
@piiq piiq commented Jun 4, 2025

This PR introduces an OpenRouterChatModel to enable support for OpenRouter-specific features not accessible through OpenaiChatModel, even when used via the Chat interface.

Motivation:
Key OpenRouter capabilities were inaccessible using the base OpenaiChatModel, particularly those critical for tool usage and structured output with models like Qwen and DeepSeek.

Features Implemented

  • Restrict routing to providers that fully support required features (e.g., tools, function calling, JSON mode)
  • Provider prioritization based on latency or throughput
  • Selective provider exclusion for specific calls
  • Access to reasoning tokens (for models that support this)

Notes:

  • Initial implementation was vibe-coded and later refactored to cleanly inherit from OpenaiChatModel
  • Streaming reasoning tokens are not yet supported. This issue is still unresolved. Any advice is appreciated.

Additional benefits- OpenRouter was not working with the @prompt decorator, not it is.

Here are a few screenshots demoing the functionality:

@prompt decorator
image

Model return type
image

Usage with RetryChatModel
image

Streaming response
image

Reasoning tokens + streaming response
image

Copy link
Owner
@jackmpcollins jackmpcollins left a comment

Choose a reason for hiding this comment

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

Looks good to me! Some typechecking errors from mypy in the failing github action - I think many will be from overriding the OpenaiChatModel methods and require ignore comments to silence.

@piiq
Copy link
Contributor Author
piiq commented Jun 7, 2025

Thanks for the feedback and the suggestions @jackmpcollins . I've updated the PR. Mypy is not expected to complain anymore.
image

@piiq
Copy link
Contributor Author
piiq commented Jun 19, 2025

Hey @jackmpcollins , would it be possible approve the workflow run to validate that I've fixed the linting errors and review/merge this PR?
Would love to see this as a part of magentic

@jackmpcollins
8000 Copy link
Owner

Thanks for the PR!

@jackmpcollins jackmpcollins merged commit bf58d7a into jackmpcollins:main Jun 22, 2025
1 check passed
@jackmpcollins
Copy link
Owner

@piiq Released now as part of https://github.com/jackmpcollins/magentic/releases/tag/v0.40.0

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 381E pull request may close these issues.

2 participants
0