Valid JSON with required keys

1.0 when the output parses as a JSON object containing every required key with the right type.

json_schema.pyverify_environment
def verify_environment(completion: str, metadata: dict | None = None) -> float:
    """metadata["required"] maps key -> type name: "str", "int", "float", "bool", "list", "dict"."""
    import json
    import re

    text = completion.strip()
    fenced = re.fullmatch(r"```(?:json)?\s*(.*?)\s*```", text, flags=re.S)
    if fenced:
        text = fenced.group(1)
    try:
        obj = json.loads(text)
    except (ValueError, TypeError):
        return 0.0
    if not isinstance(obj, dict):
        return 0.0
    types_ = {"str": str, "int": int, "float": (int, float), "bool": bool, "list": list, "dict": dict}
    for key, type_name in ((metadata or {}).get("required") or {}).items():
        if key not in obj:
            return 0.0
        value = obj[key]
        expected = types_.get(type_name)
        if expected is None:
            return 0.0
        if type_name in ("int", "float") and isinstance(value, bool):
            return 0.0
        if not isinstance(value, expected):
            return 0.0
        if type_name == "str" and not value.strip():
            return 0.0
    return 1.0
Valid JSON with required keys reward function (Python) — Reinforcement.tech