langchaingo icon indicating copy to clipboard operation
langchaingo copied to clipboard

llms: allow specifying JSON Schema

Open AkihiroSuda opened this issue 7 months ago • 3 comments

This commit adds llms.WithJSONSchema(schema *jsonschema.Definition) so as to enforce structured outputs: https://ollama.com/blog/structured-outputs

This fixes an issue that an LLM produces JSON with an unexpected schema.

Supported LLMs: ollama, openai

PR Checklist

  • [X] Read the Contributing documentation.
  • [X] Read the Code of conduct documentation.
  • [X] Name your Pull Request title clearly, concisely, and prefixed with the name of the primarily affected package you changed according to Good commit messages (such as memory: add interfaces for X, Y or util: add whizzbang helpers).
  • [X] Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • [X] Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. Fixes #123).
  • [X] Describes the source of new concepts.
  • (Not applicable) References existing implementations as appropriate.
  • [X] Contains test coverage for new functions.
  • [X] Passes all golangci-lint checks.

AkihiroSuda avatar Jun 06 '25 08:06 AkihiroSuda

Any plan to merge this PR? I would be interested too.

alegrey91 avatar Oct 14 '25 21:10 alegrey91

Rebased to solve the merge conflict

AkihiroSuda avatar Oct 15 '25 07:10 AkihiroSuda

@tmc Could you take a look?

AkihiroSuda avatar Oct 29 '25 09:10 AkihiroSuda

@tmc May I ask if you have an interest in this, or is this not planned to be merged?

AkihiroSuda avatar Dec 03 '25 02:12 AkihiroSuda

Quite interested and I want to see it in. I've been developing a supplemental project that has kept me away from maintenance tasks here but I will look this evening.

tmc avatar Dec 03 '25 02:12 tmc

I have https://github.com/tmc/langchaingo/tree/prep-v0.1.15 pushed which has structured output changes that improve provider support but I like what I see here, need to get to bed but will consider refactoring this PR to fit on top of those commits (or you can do so yourself). Thanks for your contirbution!

tmc avatar Dec 03 '25 06:12 tmc