python --version
pip install openai pydantic
export OPENAI_API_KEY=sk-...
Create a guardrail layer that validates user input before it reaches the LLM and validates LLM output before returning to the user.
1 from pydantic import BaseModel 2 from openai import OpenAI 3 4 client = OpenAI() 5 6 class SafeResponse(BaseModel): 7 answer: str 8 confidence: str # "high" | "medium" | "low" 9 10 def input_guard(user_message: str) -> str | None: 11 """Returns None if safe, or an error message string if flagged.""" 12 result = client.moderations.create(input=user_message) 13 if result.results[0].flagged: 14 return "I can't help with that request." 15 return None 16 17 def safe_chat(user_message: str) -> str: 18 # Input guard 19 guard_result = input_guard(user_message) 20 if guard_result: 21 return guard_result 22 23 # LLM call with structured output 24 resp = client.beta.chat.completions.parse( 25 model="gpt-4o-mini", 26 messages=[ 27 {"role": "system", "content": "Answer helpfully and concisely."}, 28 {"role": "user", "content": user_message}, 29 ], 30 response_format=SafeResponse, 31 ) 32 parsed = resp.choices[0].message.parsed 33 if parsed is None: 34 return "Unable to generate a response." 35 36 return f"{parsed.answer} (confidence: {parsed.confidence})" 37 38 # Test 39 print(safe_chat("What is machine learning?")) 40 print(safe_chat("How do I make a bomb?")) # Should be blocked 41
Sign in to share your feedback and join the discussion.