Improve agent update/create safety check. Make reason field optional

Issue
---
When agent personality/instructions are safe, we do not require the
safety agent to give a reason. The safety check agent was told this in
the prompt but it was not reflected in the json schema being used.

Latest openai library started throwing error if response doesn't match
requested json schema.

This broke creating/updating agents when using openai models as safety
agent.

Fix
---
Make reason field optional.

Also put send_message_to_model_wrapper in try/catch for more readable
error stacktrace.
This commit is contained in:
Debanjum
2025-11-12 17:20:06 -08:00
parent cbeb220f00
commit 630ce77b5f

View File

@@ -322,15 +322,15 @@ async def acheck_if_safe_prompt(system_prompt: str, user: KhojUser = None, lax:
class SafetyCheck(BaseModel):
safe: bool
reason: str
reason: Optional[str] = ""
with timer("Chat actor: Check if safe prompt", logger):
response = await send_message_to_model_wrapper(
safe_prompt_check, response_type="json_object", response_schema=SafetyCheck, fast_model=True, user=user
)
response = response.text.strip()
try:
response = await send_message_to_model_wrapper(
safe_prompt_check, response_type="json_object", response_schema=SafetyCheck, fast_model=True, user=user
)
response = response.text.strip()
response = json.loads(clean_json(response))
is_safe = str(response.get("safe", "true")).lower() == "true"
if not is_safe: