-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
fix(anthropic): strip undocumented keys from metadata before sending to API #24661
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1421,6 +1421,16 @@ def transform_request( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| optional_params["metadata"] = {"user_id": _litellm_metadata["user_id"]} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## Ensure metadata only contains user_id (only documented field in Anthropic Messages API) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "metadata" in optional_params and isinstance( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| optional_params["metadata"], dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _user_id = optional_params["metadata"].get("user_id") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if _user_id is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| optional_params["metadata"] = {"user_id": _user_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| optional_params.pop("metadata") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1424
to
+1432
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Extra metadata keys are dropped silently. A user who passes
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Remove internal LiteLLM parameters that should not be sent to Anthropic API | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| optional_params.pop("is_vertex_request", None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_valid_user_idvalidation creates inconsistencyThe two existing paths that populate
metadata.user_idboth call_valid_user_id()to reject emails and phone numbers (Anthropic rejects those values — see the comment at line 1032: "anthropic fails on emails"). This new filter skips that check entirely.If a caller passes
metadata={"user_id": "user@example.com"}directly inoptional_params, the new code will forward{"user_id": "user@example.com"}to Anthropic and receive an API error, while the same value arriving via theuserparameter orlitellm_params.metadatawould be silently dropped.