MCPcopy Create free account
hub / github.com/Emmimal/control-layer / demo_2_schema_enforcement

Function demo_2_schema_enforcement

demo.py:172–215  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

170# =============================================================================
171
172def demo_2_schema_enforcement():
173 print("\n" + "=" * 60)
174 print("Demo 2: Schema Enforcement - JSON Validation with Retry")
175 print("=" * 60)
176
177 schema = ResponseSchema(
178 must_be_json=True,
179 required_keys=["summary", "confidence", "sources"],
180 max_length=500,
181 )
182 config = ControlLayerConfig(total_tokens=800, max_attempts=3, base_delay_ms=50)
183
184 llm = MockLLM(failure_rate=0.75, failure_mode=FailureMode.SCHEMA_VIOLATION)
185 layer = ControlLayer(
186 llm_fn=llm,
187 system_prompt="You are a research assistant. Always respond with a JSON object.",
188 schema=schema,
189 config=config,
190 )
191
192 queries = [
193 "Summarize how context engineering differs from RAG.",
194 "Explain token budget allocation in LLM systems.",
195 "What is prompt mutation in retry logic?",
196 "How does exponential decay work in memory systems?",
197 "What are the failure modes of naive RAG?",
198 ]
199
200 records = []
201 for q in queries:
202 packet = layer.run(
203 user_input=q,
204 constraints=["Respond only with valid JSON.", "No markdown fencing."],
205 )
206 outcome = "PASSED" if packet.validation.passed else "FAILED"
207 print(
208 f" [{outcome}] Attempts: {packet.attempts} "
209 f"Strategy: {packet.strategy_used.value} "
210 f"Score: {packet.validation.score:.2f} "
211 f"Latency: {packet.total_latency_ms:.1f}ms"
212 )
213 records.append(packet)
214
215 return records, layer.audit
216
217
218# =============================================================================

Callers 1

demo.pyFile · 0.85

Calls 5

runMethod · 0.95
ResponseSchemaClass · 0.90
ControlLayerConfigClass · 0.90
ControlLayerClass · 0.90
MockLLMClass · 0.85

Tested by

no test coverage detected