guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
19.14k stars 1.04k forks source link

JSON reserved keywords #1032

Closed hudson-ai closed 2 months ago

hudson-ai commented 2 months ago

Add a set containing every single keyword reserved by JSON schema Draft 2020-12.

"Invalid" keywords (rather, unsupported/not-yet-supported keys) are now defined as "any keyword we don't explicitly provide an implementation for that is ALSO a reserved keyword.

This should prevent us from having to manually whitelist non-keywords. Note that I think that the sets/enums of "supported" keys probably needs a second refactor... Not part of this PR though.

For posterity, here's the code I wrote to get the set of all reserved keywords:

from referencing import Registry
from referencing.retrieval import to_cached_resource
import httpx

@to_cached_resource()
def retrieve_with_httpx(url: str) -> str:
    response = httpx.get(url)
    response.raise_for_status()
    return response.text

registry = Registry(retrieve=retrieve_with_httpx)
resolved = registry.resolver().lookup("https://json-schema.org/draft/2020-12/schema")

def crawl(schema, resolver):
    props = {}
    todo = [schema]
    while todo:
        current = todo.pop()
        if "$ref" in current:
            resolved = resolver.lookup(current["$ref"])
            props.update(
                crawl(resolved.contents, resolved.resolver)
            )
        if "properties" in current:
            props.update(current["properties"])
        if "allOf" in current:
            todo.extend(current["allOf"])
        if "anyOf" in current:
            raise NotImplementedError("anyOf")
        if "oneOf" in current:
            raise NotImplementedError("oneOf")
        if "$dynamicRef" in current:
            raise NotImplementedError("$dynamicRef")

    return props

props = crawl(resolved.contents, resolved.resolver)
codecov-commenter commented 2 months ago

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 61.46%. Comparing base (78575da) to head (9fa707d). Report is 2 commits behind head on main.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1032 +/- ## ========================================== - Coverage 70.20% 61.46% -8.74% ========================================== Files 62 62 Lines 4470 4471 +1 ========================================== - Hits 3138 2748 -390 - Misses 1332 1723 +391 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.