CoolFace
Apppublic

hugging2021/json-repair-env

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

<img src="https://capsule-render.vercel.app/api?type=venom&height=220&text=JSON%20Repair%20Environment&fontSize=52&color=0:0d1117,100:1a1a2e&fontColor=58a6ff&animation=twinkling&stroke=58a6ff&strokeWidth=1&desc=OpenEnv%20%E2%80%A2%20Reinforcement%20Learning%20%E2%80%A2%20LLM%20Reliability&descSize=18&descAlignY=78&descAlign=50" width="100%"/>

<div align="center">

<img src="https://readme-typing-svg.demolab.com?font=JetBrains+Mono&weight=800&size=22&duration=3000&pause=800&color=58A6FF&center=true&vCenter=true&multiline=false&repeat=true&width=680&lines=%E2%9C%A8+Training+AI+agents+to+fix+broken+JSON...;%F0%9F%94%A7+step()++reset()++state()+%E2%80%94+OpenEnv+Spec;%F0%9F%8F%86+Scaler+%C3%97+HuggingFace+OpenEnv+Hackathon+2025;%F0%9F%A7%A0+Real-world+RL+%E2%80%94+Not+games%2C+not+toys." alt="Typing SVG" />

<br/><br/>

![OpenEnv](https://github.com/preethamjain275/json-repair-env) ![Docker](https://hub.docker.com) ![HuggingFace](https://huggingface.co/spaces/preethamjain275/json-repair-env) ![FastAPI](https://fastapi.tiangolo.com) ![Python](https://python.org) ![License](LICENSE)

<br/>

![Meta](https://meta.com) ![HuggingFace Engineers](https://huggingface.co) ![Hackathon](https://huggingface.co)

</div>


<div align="center">

๐Ÿ’ก "Every LLM-powered product breaks JSON. This environment trains agents to fix it โ€” automatically."

</div>


๐Ÿ”ฅ What Is This?

<table> <tr> <td width="50%">

๐Ÿšจ The Problem

json
{
  "name": "Alice",
  "age": "30",        โ† wrong type
  "active": "true",   โ† wrong type
                      โ† missing field!
}                     โ† trailing comma

</td> <td width="50%">

โœ… The Solution

json
{
  "name": "Alice",
  "age": 30,
  "active": true,
  "role": "user"
}

</td> </tr> </table>

This OpenEnv RL environment rewards agents for transforming broken JSON into valid, schema-compliant, semantically correct output. It models a real production problem faced by every team deploying LLMs at scale.

๐ŸŽฎ Environment Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    JSON REPAIR ENVIRONMENT                      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚   ๐Ÿค– Agent                    ๐ŸŒ OpenEnv Server                 โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   reset()       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”‚
โ”‚   โ”‚         โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ โ”‚  Initial Observation  โ”‚         โ”‚
โ”‚   โ”‚         โ”‚ โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚  โ€ข broken JSON        โ”‚         โ”‚
โ”‚   โ”‚         โ”‚                 โ”‚  โ€ข target schema      โ”‚         โ”‚
โ”‚   โ”‚  BRAIN  โ”‚   step(action)  โ”‚  โ€ข hint               โ”‚         โ”‚
โ”‚   โ”‚         โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค         โ”‚
โ”‚   โ”‚         โ”‚ โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚  Reward + Next Obs    โ”‚         โ”‚
โ”‚   โ”‚         โ”‚                 โ”‚  0.0 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ 1.0  โ”‚         โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   state()       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ Three Tasks โ€” Easy โ†’ Medium โ†’ Hard

<div align="center">

TaskDifficultyChallengeReward Ceiling
๐ŸŸขeasy_syntax_fixEasyRemove trailing commas1.00
๐ŸŸกmedium_type_repairMediumFix types + add missing fields1.00
๐Ÿ”ดhard_nested_reconstructionHardReconstruct corrupted nested JSON1.00

</div>

Task 1 โ€” ๐ŸŸข Easy: Syntax Fix

python
# Input (broken)
'{"name": "Alice", "age": 30, "email": "alice@example.com",}'
#                                                           โ†‘ trailing comma

# Expected output (fixed)
'{"name": "Alice", "age": 30, "email": "alice@example.com"}'

Task 2 โ€” ๐ŸŸก Medium: Type Repair

python
# Input (broken)
'{"user": "Bob", "score": "95", "active": "true"}'
#                          โ†‘ str     โ†‘ str  missing "role" field โ†‘

# Expected output (fixed)
'{"user": "Bob", "score": 95, "active": true, "role": "user"}'

Task 3 โ€” ๐Ÿ”ด Hard: Nested Reconstruction

python
# Input (broken)
"{product: 'Laptop', price: '999.99', specs: {ram: '16gb', storage: 512}}"
# โ†‘ unquoted keys  โ†‘ wrong type   โ†‘ missing field  โ†‘ needs normalization

# Expected output (fixed)
'{"product": "Laptop", "price": 999.99, "specs": {"ram": "16GB", "storage": 512}, "available": true}'

๐Ÿ† Reward Function โ€” Partial Progress Signals

Reward = f(syntax, schema, semantics)

  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  โœ…  Valid JSON syntax      โ†’   +0.40        โ”‚
  โ”‚  โœ…  Schema compliance      โ†’   +0.40        โ”‚
  โ”‚  โœ…  Exact semantic match   โ†’   +0.20        โ”‚
  โ”‚                              โ”€โ”€โ”€โ”€โ”€โ”€โ”€         โ”‚
  โ”‚  ๐Ÿ…  Maximum per task       =   1.00         โ”‚
  โ”‚                                              โ”‚
  โ”‚  ๐Ÿ’ก  Partial credit awarded for partially    โ”‚
  โ”‚      correct fixes โ€” not binary pass/fail    โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“Š Baseline Performance

<div align="center">

TaskMistral-7BGPT-4o-miniNotes
๐ŸŸข easy_syntax_fix0.951.00Straightforward for most LLMs
๐ŸŸก medium_type_repair0.750.90Requires type inference
๐Ÿ”ด hard_nested_reconstruction0.550.75Genuinely challenges frontier models
๐Ÿ“ˆ Overall Score0.750.88Out of 1.0

</div>

๐ŸŸข Easy       โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘ 95%
๐ŸŸก Medium     โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ 75%
๐Ÿ”ด Hard       โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ 55%

๐Ÿงฉ OpenEnv Spec Compliance

โ–ถ Action Space

python
class Action(BaseModel):
    repaired_json: str    # Agent's fixed JSON string
    explanation:  str     # Brief description of changes (optional)

๐Ÿ‘ Observation Space

python
class Observation(BaseModel):
    broken_json:   str        # The malformed JSON to repair
    target_schema: dict       # JSON Schema output must comply with
    hint:          str        # What type of error to look for
    task_name:     str        # Current task identifier
    step_number:   int        # Current step in episode
    total_tasks:   int        # Total tasks in episode

๐Ÿ” API Endpoints

MethodEndpointDescription
POST/resetStart new episode, get first observation
POST/stepSubmit action, get reward + next observation
GET/stateGet current environment state
GET/healthHealth check ping
GET/tasksList all available tasks

๐Ÿš€ Quick Start

1๏ธโƒฃ Clone & Run Locally

bash
git clone https://huggingface.co/spaces/preethamjain275/json-repair-env
cd json-repair-env
pip install -r requirements.txt
uvicorn main:app --host 0.0.0.0 --port 7860

2๏ธโƒฃ Test the API

bash
# โœ… Health check
curl http://localhost:7860/health

# ๐Ÿ”„ Reset environment
curl -X POST http://localhost:7860/reset | python -m json.tool

# ๐Ÿ“ค Submit a repair action
curl -X POST http://localhost:7860/step \
  -H "Content-Type: application/json" \
  -d '{"repaired_json": "{\"name\": \"Alice\", \"age\": 30, \"email\": \"alice@example.com\"}", "explanation": "removed trailing comma"}'

3๏ธโƒฃ Run Baseline Inference

bash
export HF_TOKEN=hf_your_token_here
export API_BASE_URL=https://api-inference.huggingface.co/v1
export MODEL_NAME=mistralai/Mistral-7B-Instruct-v0.3
export ENV_URL=http://localhost:7860

python inference.py
# {"type": "START", "task": "json_repair_all_tasks", ...}
# {"type": "STEP",  "step": 1, "reward": 0.95, "done": false, ...}
# {"type": "END",   "success": true, "score": 0.75, ...}

4๏ธโƒฃ Docker

bash
docker build -t json-repair-env .
docker run -p 7860:7860 \
  -e HF_TOKEN=hf_xxx \
  -e API_BASE_URL=https://api-inference.huggingface.co/v1 \
  -e MODEL_NAME=mistralai/Mistral-7B-Instruct-v0.3 \
  json-repair-env

5๏ธโƒฃ OpenEnv Validate

bash
pip install openenv-core
openenv validate .
# โœ“ openenv.yaml valid
# โœ“ /reset responds correctly
# โœ“ /step responds correctly
# โœ“ /state responds correctly
# โœ“ All checks passed!

๐Ÿ“ Project Structure

json-repair-env/
โ”‚
โ”œโ”€โ”€ ๐Ÿ main.py          โ† FastAPI server (OpenEnv endpoints)
โ”œโ”€โ”€ ๐ŸŽฏ tasks.py         โ† Task definitions: easy / medium / hard
โ”œโ”€โ”€ โš–๏ธ  grader.py        โ† Deterministic reward & scoring logic
โ”œโ”€โ”€ ๐Ÿค– inference.py     โ† Baseline inference script (REQUIRED)
โ”œโ”€โ”€ ๐Ÿ“‹ openenv.yaml     โ† OpenEnv spec metadata
โ”œโ”€โ”€ ๐Ÿ“ฆ requirements.txt โ† Python dependencies
โ”œโ”€โ”€ ๐Ÿณ Dockerfile       โ† Container configuration
โ””โ”€โ”€ ๐Ÿ“– README.md        โ† This file

๐ŸŒ Real-World Impact

๐Ÿญ  Production LLM Apps        โ†’ Self-healing JSON parsing
๐Ÿ”Œ  Function Calling Pipelines  โ†’ Reliable structured outputs
๐Ÿ“Š  Data Extraction Workflows   โ†’ Automatic error correction
๐Ÿค–  Agentic Systems             โ†’ Resilient tool-call parsing

โš™๏ธ Environment Variables

VariableDescriptionExample
API_BASE_URLLLM API endpointhttps://api-inference.huggingface.co/v1
MODEL_NAMEModel identifiermistralai/Mistral-7B-Instruct-v0.3
HF_TOKENHuggingFace API keyhf_xxxxxxxxxxxx
ENV_URLEnvironment server URLhttp://localhost:7860

<div align="center">

๐Ÿ… Built For

<br/>

Scaler ร— HuggingFace โ€” OpenEnv Hackathon 2026

Reviewed by engineers from Meta & HuggingFace ๐Ÿค—

<br/>

![GitHub](https://github.com/preethamjain275) ![HuggingFace](https://huggingface.co/preethamjain275)

<br/>

โญ Star this repo if it helped you!

</div>

<img src="https://capsule-render.vercel.app/api?type=waving&color=gradient&customColorList=6,11,20&height=100&section=footer&animation=twinkling" width="100%"/>