Working with NJsonSchema to make it more similar to Python w Pydantic and Node with Zod #190
twf-datacom
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I saw that the example for math reasoning in the readme specifies the json schema as a string, and then parses the response back out into a json document.
I would like to be able to specify the schema with normal c# classes/records, and parse them back out into those same records. That is how the python and node sdks work.
I tried to use NJsonSchema for this and had some success after doing a few workarounds. I'll share that here. Would the maintainers consider including this technique in the lib, or would you rather not introduce another dependency in NJsonSchema? It might be enough to just include examples, and users can add NJsonSchema if they want.
First, I specify the response format records. Note you need to use the Required annotation to make openai happy. I'm new to c# so please be kind:
Then you need to set some json schema generator settings, including a custom processor to remove the 'minLength' because openai doesn't like that either:
For the min length stripper, I just got gpt to generate this:
There are probably some other json schema properties that openai doesn't like. But I think this is pretty cool because it lets you deal with end to end DTOs rather than loose json documents and strings. I was able to propagate these types up to an api via swagger/swashbuckle, and then generate client typescript code so we get the dtos in the client.
Beta Was this translation helpful? Give feedback.
All reactions