Skip to content

Improve Gemini Response Reliability #1148

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

Conversation

debanjum
Copy link
Member

@debanjum debanjum commented Apr 2, 2025

  • Specify min, max number of list items expected in AI response via json schema enforcement
  • Warn and drop invalid/empty messages when format messages for Gemini models
  • Make Gemini response adhere to the order of the schema property definitions
  • Improve agent creation safety checker by using response schema, better prompt

@debanjum debanjum force-pushed the specify-expected-items-count-via-json-schema-for-gemini-models branch 3 times, most recently from 670c4ff to a88ec1d Compare April 2, 2025 18:49
Previously we were setting message content part with empty text. This
results in error from Gemini API. Warn and drop such messages instead.

Log empty message content found during construction to root-cause the
issue but allow Khoj to respond without the offending messages in
context for call to Gemini API.
@debanjum debanjum force-pushed the specify-expected-items-count-via-json-schema-for-gemini-models branch from a88ec1d to 53d436e Compare April 2, 2025 19:41
debanjum added 2 commits April 3, 2025 01:19
Require at least 1 item in lists. Otherwise gemini flash will
sometimes return an empty list. For chat actors where max items is
known, set that as well.

OpenAI API does not support specifying min, max items in response
schema lists, so drop those properties when response schema is
passed. Add other enforcements to response schema to comply with
response schema format expected by OpenAI API.
…itions

Without explicitly using the property ordering field, gemini returns
responses in alphabetically sorted property order.

We want the model to respect the schema property definition order.
This ensures control during development to maintain response quality.

For example in CoT make it fill scratchpad before answers.
@debanjum debanjum force-pushed the specify-expected-items-count-via-json-schema-for-gemini-models branch from 53d436e to aab0107 Compare April 2, 2025 19:50
@debanjum debanjum added the maintain Maintain code, documentation or project label Apr 2, 2025
@debanjum debanjum merged commit c2b9006 into master Apr 3, 2025
11 checks passed
@debanjum debanjum deleted the specify-expected-items-count-via-json-schema-for-gemini-models branch April 3, 2025 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintain Maintain code, documentation or project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant