cue icon indicating copy to clipboard operation
cue copied to clipboard

jsonschema.Marshal

Open eadlam opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. When exporting a config, I sometimes need the value of a field to equal the json-schema of another cue type. For example: LLM tool definitions.

Describe the solution you'd like Similar to json.Marshal, I would like a jsonschema.Marshal that takes a cue definition and outputs json-schema.

Describe alternatives you've considered Alternatively, I have to maintain json-schema by hand, or do a two-phase build process.

eadlam avatar Sep 24 '24 20:09 eadlam

Do you mean generating jsonschema from CUE? If so, this would be https://github.com/cue-lang/cue/issues/929.

mvdan avatar Sep 25 '24 13:09 mvdan

I want to generate jsonschema or openapi in cue via a function like json.Marshal.

For example:

test.cue

import "encoding/jsonschema"

#ThinkTool: {
    name: string
    description: string
}

Component: {
    id: "LLMComponent"
    parameters: {
        model: string | *"gpt-4o"
        tools: {
            think_tool: jsonschema.Marshal(#ThinkTool)
        }
    } 
}

cue export test.cue

{
    "Component": {
        "id": "LLMInterface",
        "parameters": {
            "model": "gpt-4o",
            "tools": {
                "think_tool": "{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"Generated by cue.\",\"version\":\"no version\"},\"paths\":{},\"components\":{\"schemas\":{\"Thought\":{\"type\":\"object\",\"required\":[\"name\",\"description\"],\"properties\":{\"name\":{\"type\":\"string\"},\"description\":{\"type\":\"string\"}}}}}}"
            }
        }
    }
}

eadlam avatar Oct 10 '24 17:10 eadlam

I see, thanks. I would suggest that we focus on https://github.com/cue-lang/cue/issues/929 to track supporting encoding CUE schemas to JSON Schema; once that is in place, adding a package to be imported and used from CUE code should be pretty straightforward, so it doesn't need to be tracked separately. As a general rule of thumb, we expose encodings as CUE standard library APIs.

mvdan avatar Mar 05 '25 15:03 mvdan