Skip to content

feat(guardrails): added guardrails block & docs - #1589

Closed
devangjhabakh wants to merge 1 commit into
simstudioai:stagingfrom
devangjhabakh:feat/djj/guardrails_block
Closed

feat(guardrails): added guardrails block & docs#1589
devangjhabakh wants to merge 1 commit into
simstudioai:stagingfrom
devangjhabakh:feat/djj/guardrails_block

Conversation

@devangjhabakh

Copy link
Copy Markdown

Summary

Brief description of what this PR does and why.

Fixes #(issue)

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

How has this been tested? What should reviewers focus on?

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Screenshots/Videos

@vercel

vercelBot commented Oct 10, 2025

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Sim Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-appsgreptile-appsBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Overview

Summary

This PR adds a comprehensive guardrails block that validates content using four different validation types: JSON validation, regex pattern matching, hallucination detection via RAG+LLM, and PII detection using Microsoft Presidio.

Key Changes:

  • New guardrails block with conditional UI for different validation types
  • API route at /api/guardrails/validate that routes to appropriate validators
  • JSON and regex validators implemented in TypeScript
  • Hallucination detector queries knowledge base and uses LLM to score content confidence (0-10 scale)
  • PII detector wraps Microsoft Presidio Python library via subprocess, supporting both block and mask modes
  • Docker integration adds Python 3, pip, and runs setup script to install Presidio dependencies
  • New grouped-checkbox-list component for selecting PII entity types
  • Comprehensive documentation in MDX format

Issues Found:

  • Typo in route.ts line 145: "strfing" should be "string" (comment syntax)
  • Timeout handling in validate_pii.ts resolves instead of rejecting, making timeouts indistinguishable from normal failures (comment logic)

Confidence Score: 4/5

  • This PR is safe to merge after fixing the typo - the logic issue is minor
  • Score of 4 reflects well-structured implementation with proper error handling, comprehensive documentation, and Docker integration. Two minor issues found: a typo that needs fixing (syntax error) and a timeout handling inconsistency (logic issue). The core functionality is solid with good separation of concerns between TypeScript and Python components.
  • Fix the typo in apps/sim/app/api/guardrails/validate/route.ts line 145. Consider reviewing timeout handling in apps/sim/lib/guardrails/validate_pii.ts line 83 for better error propagation.

Important Files Changed

File Analysis

FilenameScoreOverview
apps/sim/blocks/blocks/guardrails.ts4/5Adds comprehensive guardrails block configuration with 4 validation types (JSON, regex, hallucination, PII). Includes proper conditional field display and extensive PII entity types list.
apps/sim/app/api/guardrails/validate/route.ts3/5New API route for guardrails validation. Contains minor typo on line 145 ("strfing"). Properly handles validation types and error cases.
apps/sim/lib/guardrails/validate_pii.ts3/5TypeScript wrapper for Python PII detection. Timeout handling resolves instead of rejecting (line 83), making error types indistinguishable. Otherwise solid implementation.
apps/sim/lib/guardrails/validate_hallucination.ts4/5RAG-based hallucination detection using LLM scoring. Queries knowledge base and uses LLM to score content confidence (0-10 scale). Solid implementation with proper error handling.
docker/app.Dockerfile4/5Adds Python 3 and pip to runner stage, copies guardrails files, runs setup script to install Presidio dependencies. Proper integration with existing Docker build.

Sequence Diagram

sequenceDiagram
participant User
participant GuardrailsBlock
participant ValidateAPI
participant JSONValidator
participant RegexValidator
participant HallucinationValidator
participant KnowledgeBase
participant LLM
participant PIIValidator
participant PresidioPython
User->>GuardrailsBlock: Configure validation type
User->>GuardrailsBlock: Execute workflow
GuardrailsBlock->>ValidateAPI: POST /api/guardrails/validate
alt JSON Validation
ValidateAPI->>JSONValidator: validateJson(input)
JSONValidator->>JSONValidator: JSON.parse(input)
JSONValidator-->>ValidateAPI: {passed, error?}
else Regex Validation
ValidateAPI->>RegexValidator: validateRegex(input, pattern)
RegexValidator->>RegexValidator: new RegExp(pattern).test(input)
RegexValidator-->>ValidateAPI: {passed, error?}
else Hallucination Check
ValidateAPI->>HallucinationValidator: validateHallucination(params)
HallucinationValidator->>KnowledgeBase: Query for relevant chunks
KnowledgeBase-->>HallucinationValidator: Return context chunks
HallucinationValidator->>LLM: Score confidence (0-10)
LLM-->>HallucinationValidator: {score, reasoning}
HallucinationValidator->>HallucinationValidator: Check score >= threshold
HallucinationValidator-->>ValidateAPI: {passed, score, reasoning}
else PII Detection
ValidateAPI->>PIIValidator: validatePII(params)
PIIValidator->>PresidioPython: spawn python process
PIIValidator->>PresidioPython: Send input via stdin
PresidioPython->>PresidioPython: Presidio analyze/anonymize
PresidioPython-->>PIIValidator: JSON result via stdout
PIIValidator-->>ValidateAPI: {passed, detectedEntities, maskedText?}
end
ValidateAPI-->>GuardrailsBlock: Validation result
GuardrailsBlock-->>User: Display result & continue workflow
Loading

23 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment threadapps/sim/app/api/guardrails/validate/route.ts
Comment threadapps/sim/lib/guardrails/validate_pii.ts
@waleedlatif1waleedlatif1 changed the title Adding guardrails blockfeat(guardrails): added guardrails block & docsOct 11, 2025
@waleedlatif1
waleedlatif1 changed the base branch from main to stagingOctober 11, 2025 19:45
@waleedlatif1

Copy link
Copy Markdown
Collaborator

updated version in #1605

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@devangjhabakh@waleedlatif1