Improve guardrail: structured outputs + LLM-generated user messages#46
Open
dcschreiber wants to merge 2 commits intomainfrom
Open
Improve guardrail: structured outputs + LLM-generated user messages#46dcschreiber wants to merge 2 commits intomainfrom
dcschreiber wants to merge 2 commits intomainfrom
Conversation
…ction
The guardrail LLM now returns {decision, reason, message} where message
is sent directly to the user and reason is kept for logging/tracing.
Removes hardcoded GUARDRAIL_REJECTION_MESSAGE and related constants.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replaces prompt-based JSON enforcement with Anthropic's output_config JSON schema, guaranteeing valid responses with constrained decision values. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
The preview deployment for sefaria/ai-chatbot:client is ready. 🟢 Open Preview | Open Build Logs | Open Application Logs Last updated at: 2026-02-24 13:45:08 CET |
saengel
approved these changes
Feb 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We changed the Guardrail prompt to also return a message. that message is returned to the user when the Guardrail blocks.
Claude:
Summary
output_configwith a JSON schema to guarantee valid{decision, reason, message}responses from the API, eliminating parsing edge cases (markdown fences, malformed JSON).messagefield that is sent directly to the user when blocked, replacing the hardcoded rejection text. This allows the response to be contextual and more helpful.strip_markdown_fences, hardcoded rejection constants (GUARDRAIL_REJECTION_MESSAGE,GUARDRAIL_UNAVAILABLE_REASON, etc.), and the conditional logic inclaude_service.pythat chose between generic and reason-based rejections.Changed files
server/chat/V2/guardrail/guardrail_service.py— AddedGUARDRAIL_OUTPUT_SCHEMA,output_configin API call,messagefield onGuardrailResult, simplified_parse_responseserver/chat/V2/agent/claude_service.py— Removed rejection message formatting logic; usesguardrail_result.messagedirectlyserver/chat/V2/prompts/prompt_fragments.py— Removed unused guardrail constantsserver/chat/tests/test_guardrail_service.py— Updated tests for new schema; addedtest_output_config_passed_to_apiandtest_reason_field_preservedTest plan
pytest server/chat/tests/test_guardrail_service.py)🤖 Generated with Claude Code