Skip to content

Expose model preferences in Context.sample for flexible model selection. #542

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 4 commits into from
May 22, 2025

Conversation

davenpi
Copy link
Contributor

@davenpi davenpi commented May 21, 2025

This PR addresses #524 by exposing the model_preferences parameter in the Context.sample method. This allows server authors to specify model selection preferences—such as model hints or a full ModelPreferences object—when requesting LLM sampling from the client.

Key changes

  • Added model_preferences parameter to Context.sample, supporting:
    • A string (single model hint)
    • A list of strings (multiple model hints)
    • A ModelPreferences object (for advanced use)
  • Added robust input validation and conversion logic.
  • Updated documentation and usage examples to reflect the new parameter.
  • Added unit tests for all supported input types and error cases.

Docs

  • Updated the docs to include the new parameter

Let me know if you'd like any changes or more tests!

@Copilot Copilot AI review requested due to automatic review settings May 21, 2025 23:43
@github-actions github-actions bot added documentation Improvements or additions to documentation tests labels May 21, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR exposes a new model_preferences parameter in Context.sample to allow callers to specify model selection hints or full preferences, with input validation and updated docs/tests.

  • Introduced model_preferences parameter in Context.sample (supports str, list[str], ModelPreferences, or None)
  • Added _parse_model_preferences helper with validation and conversion logic
  • Updated tests and documentation to cover the new parameter

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/server/test_context.py Added fixture, imports, and tests for parsing model_preferences inputs
src/fastmcp/server/context.py Added model_preferences argument to sample and parsing helper method
docs/servers/context.mdx Updated example usage and method signature to include model_preferences
Comments suppressed due to low confidence (1)

tests/server/test_context.py:86

  • Add a test case for None input to verify that _parse_model_preferences(None) returns None without error.
def test_parse_model_preferences_invalid_type(self, context):

Co-authored-by: Copilot <[email protected]>
Copy link
Owner

@jlowin jlowin left a comment

Choose a reason for hiding this comment

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

Thanks @davenpi! Made a minor tweak to add typing to the helper function arg, otherwise LGTM

@jlowin jlowin added the enhancement New feature or request label May 22, 2025
@jlowin jlowin merged commit de581e4 into jlowin:main May 22, 2025
4 checks passed
@davenpi
Copy link
Contributor Author

davenpi commented May 22, 2025

Awesome. Thanks!

@davenpi davenpi deleted the set-model-preferences branch May 22, 2025 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants