Skip to content

Commit f8eaa40

Browse files
authored
chore: better error messages for moderations API (#3887)
# What does this PR do? ## Test Plan ``` ~/projects/lst3 remotes/origin/HEAD* .venv ❯ curl http://localhost:8321/v1/moderations \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini", "input": [ "hello" ] }' {"detail":"Invalid value: No shield associated with provider_resource id gpt-4o-mini: choose from ['together/meta-llama/Llama-Guard-4-12B']"} ```
1 parent 30ba8c8 commit f8eaa40

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

llama_stack/core/routers/safety.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ async def get_shield_id(self, model: str) -> str:
6565
"""Get Shield id from model (provider_resource_id) of shield."""
6666
list_shields_response = await self.routing_table.list_shields()
6767

68-
matches = [s.identifier for s in list_shields_response.data if model == s.provider_resource_id]
68+
matches: list[str] = [s.identifier for s in list_shields_response.data if model == s.provider_resource_id]
6969

7070
if not matches:
71-
raise ValueError(f"No shield associated with provider_resource id {model}")
71+
raise ValueError(
72+
f"No shield associated with provider_resource id {model}: choose from {[s.provider_resource_id for s in list_shields_response.data]}"
73+
)
7274
if len(matches) > 1:
73-
raise ValueError(f"Multiple shields associated with provider_resource id {model}")
75+
raise ValueError(
76+
f"Multiple shields associated with provider_resource id {model}: matched shields {matches}"
77+
)
7478
return matches[0]
7579

7680
shield_id = await get_shield_id(self, model)

0 commit comments

Comments
 (0)