New: Audio API, Embeddings & Realtime WebSocket now available!
osmAPI LogoosmAPI

Structural Remediation & Healing

Automatically sanitize and repair malformed JSON payloads to ensure predictable interface compliance.

The Structural Remediation Engine

osmAPI features a sophisticated response-healing plugin designed to safeguard your application's data integrity. This "Structure Shield" automatically identifies, sanitizes, and repairs malformed JSON responses from model providers, ensuring that every transaction conforms to your specified schema even when the underlying generator fails to maintain perfect formatting.

The Challenge: Linguistic Inaccuracy

Large language models (LLMs) frequently deviate from strict JSON standards during complex inference tasks:

  • Markdown Interference: Models often encapsulate structured data within ```json blocks.
  • Narrative Overspill: Explanatory text may precede or succeed the intended data payload.
  • Stochastic Errors: Issues like unquoted keys, trailing commas, or incorrect quotation marks.
  • Premature Termination: High-latency or token-limited responses may be truncated mid-object.

Structural Remediation eliminates the need for brittle, localized error handling by centralizing these fixes at the gateway layer.


Activating the Structure Shield

To engage the remediation engine, include response-healing in the plugins manifest of your request:

curl -X POST "https://api.osmapi.com/v1/chat/completions" \
  -H "Authorization: Bearer $OSM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{ "role": "user", "content": "Extract data into a JSON object." }],
    "response_format": { "type": "json_object" },
    "plugins": [{ "id": "response-healing" }]
  }'

Policy Constraint: Remediation is exclusively active when the response_format is set to json_object or json_schema. Standard creative text responses are delivered without structural modification.


Remediation Methodologies

The engine applies a multi-layered repair strategy to maximize data recovery:

1. Isolated Extraction

Isolates valid JSON signatures from surrounding linguistic noise or Markdown decorators.

  • Input: "Here is the result: {"status": "ok"}"
  • Result: {"status": "ok"}

2. Syntax Normalization

Corrects common formatting violations to ensure standard RFC 8259 compliance.

  • Trailing Segments: {"data": 100,}{"data": 100}
  • Quotation Alignment: {'name': 'Alpha'}{"name": "Alpha"}
  • Key Validation: {active: true}{"active": true}

3. Truncation Closure

If a response is severed due to token limits, the engine intelligently determines and appends the necessary closing delimiters to maintain structural balance.

  • Input: {"user": {"id": 101, "role": "admin"
  • Result: {"user": {"id": 101, "role": "admin"}}

Practical Deployment Scenarios

Dynamic Integration with JSON Schema

Combine remediation with strict typing for production-grade reliability:

const completion = await gateway.chat.completions.create({
	model: "gpt-4o",
	messages: [{ role: "user", content: "Generate primary user profile." }],
	response_format: {
		type: "json_schema",
		json_schema: {
			name: "identity_profile",
			schema: {
				type: "object",
				required: ["username", "clearance"],
				properties: {
					username: { type: "string" },
					clearance: { type: "integer" },
				},
			},
		},
	},
	plugins: [{ id: "response-healing" }],
});

Observability & Tuning

osmAPI audits every remediation event. You can inspect the specific "Healing Strategy" applied via the transaction logs:

Remediation ModeAction Insight
markdown_extractionExtracted JSON from code-block indicators.
mixed_content_extractionExtracted JSON from surrounding text.
syntax_fixFixed commas, quotes, or key assignments.
truncation_completionAppended delimiters for truncated outputs.
combined_strategiesApplied multiple healing strategies in sequence.

Operational Constraints

Consistency Note: Structural Remediation is optimized for non-streaming transactions. Streaming outputs are delivered in raw form as they arrive from the provider to prioritize low latency.

Capability Boundaries

While highly effective, the engine cannot reconstruct data that was never generated. If a response contains purely nonsensical or non-recognizable patterns, the raw output will be delivered for manual review.


Strategic Best Practices

  • Zero-Trust Validation: Continue to implement basic type-checking in your application layer as a secondary safeguard.
  • Schema Simplification: Use flatter, more intuitive JSON hierarchies to improve the model's natural accuracy.
  • Performance Auditing: Monitor "Healing Rates" in your dashboard; if a specific model requires frequent remediation, consider calibrating your prompt instructions for better structural affinity.
  • Model Selection: Higher-tier models (e.g., GPT-4o, Claude 3.5 Sonnet) inherently produce cleaner JSON and require less frequent remediation.

How is this guide?