Skip to content

Conversation

@dbuos
Copy link
Contributor

@dbuos dbuos commented May 30, 2025

  • Add automatic model detection for JSON schema support
  • Implement graceful fallback to function calling for unsupported models
  • Maintain full backward compatibility with existing APIs

Description

Overview

This PR implements support for OpenAI's native JSON Schema structured output capabilities while maintaining minimal architectural changes and leveraging the existing program pattern.

Approach

1. Minimal Integration Changes

  • Leverages existing structured_predict() and astructured_predict() methods
  • No changes to public APIs - fully backward compatible

Fixes # (issue)

New Package?

Did I fill in the tool.llamahub section in the pyproject.toml and provide a detailed README.md for my new integration or package?

  • Yes
  • No

Version Bump?

Did I bump the version in the pyproject.toml file of the package I am updating? (Except for the llama-index-core package)

  • Yes
  • No

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Your pull-request will likely not be merged unless it is covered by some form of impactful unit testing.

  • I added new unit tests to cover this change
  • I believe this change is already covered by existing unit tests

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added Google Colab support for the newly added notebooks.
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran uv run make format; uv run make lint to appease the lint gods

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label May 30, 2025
@dbuos dbuos requested review from Disiok and logan-markewich May 30, 2025 02:13
@dbuos dbuos force-pushed the structured_outputs branch from 84e7362 to 1378621 Compare June 2, 2025 21:43
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jun 2, 2025
@dbuos dbuos force-pushed the structured_outputs branch 2 times, most recently from 1f693aa to 0c8a87f Compare June 2, 2025 22:21
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 2, 2025
@dbuos
Copy link
Contributor Author

dbuos commented Jun 2, 2025

@logan-markewich It seems that it is now ready to merge, I remove all the added program logic and keep the modification local to the base openAI class methods, also added some unit tests. Lmk what you think

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need to import/use a private function? I thought the openai client let you directly pass in a pydantic object?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah this is probably because we are using the chat api?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, this is indeed because I'm using the chat and achat APIs.

I’m also not entirely comfortable with calling that _type_to_response_format function directly. It might be safer in the long run to extract that logic into our own utility function within the repo...

do you think that could be the best approach here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, I think this is fine for now. In a future PR we can extract out that logic into a util

@pazevedo-hyland
Copy link
Contributor

@dbuos would the idea be for this to have an interface with FunctionCalling agents while being called during Workflows?

@logan-markewich
Copy link
Collaborator

@pazevedo-hyland this is kind of separate from function agents for now

I do have an idea to add output class/structured responses to the FunctionAgent class though

  1. We recently added tool_required as an arg for all function calling LLMs
  2. From there, you could imagine an output_cls argument added to the FunctionAgent class. When set, we set tool_required=True and use the output class as a tool. The agent loop ends when this "tool" is called. This gets you a structured output with minimal LLM calls

From what I can see, openai does not let you mix response_format with tools (I might be wrong though), so the code in this PR isn't quite relevant

@dbuos dbuos force-pushed the structured_outputs branch from f318346 to 5a2acf6 Compare June 3, 2025 22:17
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jun 3, 2025
@dbuos dbuos force-pushed the structured_outputs branch 2 times, most recently from 17bb422 to 4227dee Compare June 3, 2025 22:40
@dbuos
Copy link
Contributor Author

dbuos commented Jun 3, 2025

@logan-markewich I just added support for the two streaming methods (sync and async)

- Add JSON_SCHEMA mode to PydanticProgramMode enum
- Create JsonSchemaProgram class using response_format API
- Add automatic model detection for JSON schema support
- Implement graceful fallback to function calling for unsupported models
- Maintain full backward compatibility with existing APIs
@dbuos dbuos force-pushed the structured_outputs branch from 4227dee to f6e4af4 Compare June 4, 2025 14:14
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 4, 2025
@logan-markewich logan-markewich merged commit bc2d63b into run-llama:main Jun 4, 2025
10 checks passed
@colca colca mentioned this pull request Jun 9, 2025
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants