Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OpenAI's Structured Outputs feature to prevent validation errors #514

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

renkehohl
Copy link

Used the result_tools parameter to generate a json_schema which is passed to the OpenAI client as response_format. This guides the model to format it's responses according to the given schema, preventing validation errors.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

interesting, are their any docs on why this might work better?

openai_messages = list(chain(*(self._map_message(m) for m in messages)))

model_settings = model_settings or {}

return await self.client.chat.completions.create(
response = await self.client.chat.completions.create(
Copy link
Member

Choose a reason for hiding this comment

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

you don't need this change.

else:
tool_choice = 'auto'

tool_choice = 'auto' if self.tools else None
Copy link
Member

Choose a reason for hiding this comment

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

We have to respect allow_text_result, I think right now you're not.

allow_text_result,
tools,
)
response_format = self._map_response_format(result_tools[0]) if result_tools else None
Copy link
Contributor

@dmontagu dmontagu Dec 23, 2024

Choose a reason for hiding this comment

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

If there are multiple result tools (which I believe is the case when the result type is a union), we would definitely need to ensure that all the tool calls are present in the final response_format

Copy link
Contributor

Choose a reason for hiding this comment

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

Also

Suggested change
response_format = self._map_response_format(result_tools[0]) if result_tools else None
response_format = self._map_response_format(result_tools[0]) if result_tools and not allow_text_result else None

(Or, find a way for the response format to allow raw strings)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants