Update Anthropic and OpenAI clients to use Omit instead of NotGiven#3088
Update Anthropic and OpenAI clients to use Omit instead of NotGiven#3088
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8033e82710
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| from anthropic import AsyncAnthropic, Omit | ||
| from anthropic._types import omit |
There was a problem hiding this comment.
Preserve compatibility with supported Anthropic versions
The new import path requires Omit/omit, but this module still advertises support for anthropic>=0.40.0 in pyproject.toml; earlier releases in that range only expose the older NotGiven API, so importing this handler will raise ImportError even when users satisfy FastMCP's declared dependency floor. This makes the Anthropic sampling handler unusable for a subset of supported installations unless you either raise the minimum version or provide a fallback import path.
Useful? React with 👍 / 👎.
Test Failure AnalysisSummary: The "Tests with lowest-direct dependencies" job failed because Root Cause: The PR updates the sampling handlers to use the newer
The Suggested Solution: Update the minimum version requirements in [project.optional-dependencies]
anthropic = ["anthropic>=0.41.0"] # Changed from 0.40.0
openai = ["openai>=1.54.0"] # Needs verification of exact versionAction Items:
Detailed AnalysisError Log ExcerptsAnthropic Handler Import Error: OpenAI Handler Import Error: Version ResearchAccording to the Anthropic SDK PyPI release, version 0.41.0 (released December 2024) included the change "make the Omit type public (#772)". The OpenAI SDK appears to have introduced Related Files
Comment updated to reflect the latest analysis. The test failure is due to incompatible minimum dependency versions. |
WalkthroughReplaces sentinel-based handling (NotGiven/NOT_GIVEN) in Anthropic and OpenAI sampling handlers with dynamic kwargs construction that only includes fields when their values are non-None. Defaults and internal logic were changed from NotGiven to None, toolChoice "none" is normalized to None (omitting tools), and requests are built into a kwargs dict (model, messages, max_tokens always present) with optional keys (system, temperature, stop_sequences/stop, tools, tool_choice) added conditionally before calling create(**kwargs). This shifts how absent optional fields appear in requests. Possibly related PRs
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| try: | ||
| from anthropic import AsyncAnthropic, NotGiven | ||
| from anthropic._types import NOT_GIVEN | ||
| from anthropic import AsyncAnthropic, Omit | ||
| from anthropic._types import omit |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Search for anthropic version constraints in dependency files
rg -n --glob 'pyproject.toml' --glob 'requirements*.txt' --glob 'setup.cfg' --glob 'setup.py' 'anthropic'Repository: jlowin/fastmcp
Length of output: 166
🏁 Script executed:
#!/bin/bash
# Check the actual imports at lines 21-23
head -n 30 src/fastmcp/client/sampling/handlers/anthropic.py | tail -n +15Repository: jlowin/fastmcp
Length of output: 384
🏁 Script executed:
#!/bin/bash
# List potential dependency files to ensure we capture all
fd -t f -E '.git' -E '__pycache__' \( -name 'pyproject.toml' -o -name 'requirements*.txt' -o -name 'setup.py' -o -name 'setup.cfg' -o -name 'uv.lock' -o -name 'poetry.lock' \)Repository: jlowin/fastmcp
Length of output: 288
🌐 Web query:
anthropic SDK Omit omit type introduction version
💡 Result:
In Anthropic’s Python SDK (anthropic), Omit is a sentinel type (paired with NotGiven) used to explicitly omit values—most notably to remove default headers/query params when building a request. (deepwiki.com)
The first release note that explicitly calls out adopting Omit in public type signatures is anthropic v0.68.1 (2025-09-26): “types: change optional parameter type from NotGiven to Omit”. (newreleases.io)
Citations:
- 1: https://deepwiki.com/anthropics/anthropic-sdk-python/10.2-type-system-and-code-generation?utm_source=openai
- 2: https://newreleases.io/project/pypi/anthropic/release/0.68.1?utm_source=openai
🏁 Script executed:
#!/bin/bash
# Get the full try-except block to see error handling
sed -n '21,40p' src/fastmcp/client/sampling/handlers/anthropic.pyRepository: jlowin/fastmcp
Length of output: 737
🏁 Script executed:
#!/bin/bash
# Get more of the except block
sed -n '38,45p' src/fastmcp/client/sampling/handlers/anthropic.pyRepository: jlowin/fastmcp
Length of output: 312
Bump minimum Anthropic SDK version to 0.68.1 or later.
The Omit type and omit function were introduced in Anthropic SDK v0.68.1. The current constraint allows anthropic>=0.40.0, which will fail at runtime on any version below v0.68.1 with an uninformative import error. Update pyproject.toml line 54 to specify anthropic>=0.68.1.
…K versions 🤖 Generated with Claude Code https://claude.ai/code/session_01Fs5vHiWaUebe826pGq4eCN
🤖 Generated with Claude Code https://claude.ai/code/session_01Fs5vHiWaUebe826pGq4eCN
Description
This PR updates the Anthropic and OpenAI sampling handlers to use the newer
Omittype andomitsentinel value instead of the deprecatedNotGivenandNOT_GIVENfrom their respective SDK versions.Changes Made
Anthropic Handler (
src/fastmcp/client/sampling/handlers/anthropic.py)NotGivenimport withOmitNOT_GIVENimport withomitfromanthropic._typesNotGiventoOmitNOT_GIVENtoomitOpenAI Handler (
src/fastmcp/client/sampling/handlers/openai.py)NotGivenandNOT_GIVENimports withOmitandomitNotGiventoOmitNOT_GIVENtoomitTest Updates (
tests/server/providers/openapi/test_openapi_performance.py)Anytype import for proper type hintsdict[str, Any]to schema variable# type: ignore[index]commentRationale
The
Omittype andomitsentinel are the current recommended approach in both Anthropic and OpenAI SDKs for indicating that optional parameters should be omitted from API requests. This change ensures compatibility with newer SDK versions and follows the libraries' current best practices.Contributors Checklist
Review Checklist
https://claude.ai/code/session_01Fs5vHiWaUebe826pGq4eCN