LCORE-1209: Custom shields not compatible with LCORE#1221
LCORE-1209: Custom shields not compatible with LCORE#1221tisnik merged 1 commit intolightspeed-core:mainfrom
Conversation
e85c194 to
1de20a4
Compare
WalkthroughIntroduces provider-specific validation for shield moderation: only llama-guard shields now validate that their provider_resource_id exists in available_models; other providers skip this check and proceed directly to moderation. Includes corresponding test updates to verify the new behavior for both llama-guard and non-llama-guard shields. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/utils/shields.py (2)
91-91: Extract"llama-guard"to a named constant.♻️ Proposed refactor
+LLAMA_GUARD_PROVIDER_ID = "llama-guard" + ... - if shield.provider_id == "llama-guard" and ( + if shield.provider_id == LLAMA_GUARD_PROVIDER_ID and (Also check
constants.pyto confirm this constant isn't already defined there before adding it here. As per coding guidelines: "Checkconstants.pyfor shared constants before defining new ones."🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/shields.py` at line 91, Replace the hardcoded "llama-guard" string used in the condition on shield.provider_id with a named constant: first check constants.py for an existing constant (e.g., LLAMA_GUARD_PROVIDER) and if missing add one there with the value "llama-guard"; then import that constant into src/utils/shields.py and update the conditional that references shield.provider_id (the if statement using "llama-guard") to use the constant instead.
82-82:available_modelsis fetched on every call regardless of shield provider type.
client.models.list()is an async I/O call that is only consumed inside theprovider_id == "llama-guard"branch (lines 91–94). When no llama-guard shields are configured (e.g., a pure custom-shield deployment), this round-trip is wasted on every moderation request.♻️ Lazy-load available_models only when needed
- available_models = {model.id for model in await client.models.list()} - shields = await client.shields.list() + available_models: set[str] | None = None for shield in shields: if shield.provider_id == "llama-guard" and ( not shield.provider_resource_id - or shield.provider_resource_id not in available_models + or shield.provider_resource_id + not in ( + available_models := available_models + or {model.id for model in await client.models.list()} + ) ):🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/shields.py` at line 82, Move the async call to client.models.list() out of the unconditional top-level line that builds available_models and instead fetch it lazily only when provider_id == "llama-guard" (i.e., inside the branch that actually uses available_models); replace the current eager set comprehension ({model.id for model in await client.models.list()}) with a conditional fetch or a small cached helper that awaits client.models.list() only when needed, and reference/assign to the same available_models name inside the llama-guard branch so the rest of the code can use it unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/utils/shields.py`:
- Line 91: Replace the hardcoded "llama-guard" string used in the condition on
shield.provider_id with a named constant: first check constants.py for an
existing constant (e.g., LLAMA_GUARD_PROVIDER) and if missing add one there with
the value "llama-guard"; then import that constant into src/utils/shields.py and
update the conditional that references shield.provider_id (the if statement
using "llama-guard") to use the constant instead.
- Line 82: Move the async call to client.models.list() out of the unconditional
top-level line that builds available_models and instead fetch it lazily only
when provider_id == "llama-guard" (i.e., inside the branch that actually uses
available_models); replace the current eager set comprehension ({model.id for
model in await client.models.list()}) with a conditional fetch or a small cached
helper that awaits client.models.list() only when needed, and reference/assign
to the same available_models name inside the llama-guard branch so the rest of
the code can use it unchanged.
Description
We were assuming that all shields would have a provider_resource_id, however custom shields (e.g. the ones defined in
lightspeed-providers) do not have this field, resulting in failure.Type of change
Tools used to create PR
Identify any AI code assistants used in this PR (for transparency and review context)
NA
Related Tickets & Documents
Checklist before requesting a review
Testing
Tested manually, with llama-guard shield with invalid shield name, and a custom shield
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests