[Feature] Key list endpoint: Add project_id and access_group_id filters#22356
[Feature] Key list endpoint: Add project_id and access_group_id filters#22356yuneng-jiang merged 1 commit intomainfrom
Conversation
Add filtering capabilities to /key/list endpoint for project_id and access_group_id parameters. Both filters work globally across all visibility rules and stack with existing sort/pagination params. Added comprehensive unit tests for the new filters. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryAdds
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| litellm/proxy/management_endpoints/key_management_endpoints.py | Adds project_id and access_group_id query parameters to list_keys, threads them through _list_key_helper to _build_key_filter_conditions, where they are applied as global AND filters. Clean, minimal change with correct Prisma hasSome usage for the array field. |
| tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py | Adds 3 unit tests covering project_id filtering, access_group_id filtering with hasSome, and the combination of both filters. Tests are pure mock-free unit tests verifying filter construction — no network calls. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["GET /key/list"] -->|"project_id, access_group_id params"| B["list_keys()"]
B --> C["validate_key_list_check()"]
C --> D["_list_key_helper()"]
D --> E["_build_key_filter_conditions()"]
E --> F{"Build visibility\nOR conditions"}
F --> G["user_condition\n(own keys)"]
F --> H["admin_team_ids\n(team admin keys)"]
F --> I["member_team_ids\n(service accounts)"]
G --> J{"project_id\nprovided?"}
H --> J
I --> J
J -->|Yes| K["Wrap in AND:\nwhere + project_id filter"]
J -->|No| L{"access_group_id\nprovided?"}
K --> L
L -->|Yes| M["Wrap in AND:\nwhere + hasSome filter"]
L -->|No| N["Return where\ndict to Prisma"]
M --> N
Last reviewed commit: 6d8b5b7
| if project_id: | ||
| where = {"AND": [where, {"project_id": project_id}]} | ||
| if access_group_id: | ||
| where = {"AND": [where, {"access_group_ids": {"hasSome": [access_group_id]}}]} |
There was a problem hiding this comment.
Nested AND creates deep nesting
When both project_id and access_group_id are provided, the sequential wrapping produces a doubly-nested AND: {"AND": [{"AND": [where, project_filter]}, access_group_filter]}. This works correctly with Prisma but could be flattened into a single AND list for readability and consistency with the pattern used above on line 4350.
| if project_id: | |
| where = {"AND": [where, {"project_id": project_id}]} | |
| if access_group_id: | |
| where = {"AND": [where, {"access_group_ids": {"hasSome": [access_group_id]}}]} | |
| if project_id or access_group_id: | |
| and_parts = [where] | |
| if project_id: | |
| and_parts.append({"project_id": project_id}) | |
| if access_group_id: | |
| and_parts.append({"access_group_ids": {"hasSome": [access_group_id]}}) | |
| where = {"AND": and_parts} |
Note: adopting this would require updating the combined-filter test (test_build_key_filter_project_id_and_access_group_id) to expect a flat 3-element AND list instead of nested ANDs.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Summary
Added
project_idandaccess_group_idfiltering parameters to the/key/listendpoint. Both filters are applied as global AND conditions that narrow results across all visibility rules (user keys, team keys, etc.) and work with existing sort/pagination parameters. Theaccess_group_idfilter uses Prisma'shasSomeoperator to check if a key'saccess_group_idsarray contains the specified ID.Testing
Added 3 unit tests:
test_build_key_filter_project_id: Verifies project_id creates proper AND conditiontest_build_key_filter_access_group_id: Verifies access_group_id uses hasSome for array filteringtest_build_key_filter_project_id_and_access_group_id: Verifies both filters stack correctlyAll existing tests pass with no regressions.
Type
🆕 New Feature
✅ Test