Skip to content

Add MAX_QUERY_LENGTH guard in handle_chat to prevent overly long queries (#83)#90

Open
tisha-varma wants to merge 1 commit intoINCF:mainfrom
tisha-varma:fix/query-length-guard
Open

Add MAX_QUERY_LENGTH guard in handle_chat to prevent overly long queries (#83)#90
tisha-varma wants to merge 1 commit intoINCF:mainfrom
tisha-varma:fix/query-length-guard

Conversation

@tisha-varma
Copy link
Contributor

This PR adds a configurable query length guard in handle_chat to prevent excessively long inputs from reaching the Gemini API.

Changes:

  • Introduced MAX_QUERY_LENGTH environment variable (default: 2000 chars)
  • Added early validation in handle_chat
  • Trimmed whitespace before validation
  • Added tests to verify guard behavior
  • Documented MAX_QUERY_LENGTH in .env.template

This prevents token limit errors from large inputs and provides users with a clear, friendly message.

Fixes #83

Copy link
Collaborator

@QuantumByte-01 QuantumByte-01 left a comment

Choose a reason for hiding this comment

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

Correct and clean implementation. One minor nit: int(os.getenv("MAX_QUERY_LENGTH", "2000")) at module load will raise ValueError if someone sets it to a non-integer string in .env. Consider wrapping in a try/except with a fallback. Not a blocker.

@tisha-varma tisha-varma force-pushed the fix/query-length-guard branch from f9a37fa to 4e903b1 Compare March 20, 2026 17:31
@tisha-varma
Copy link
Contributor Author

Correct and clean implementation. One minor nit: int(os.getenv("MAX_QUERY_LENGTH", "2000")) at module load will raise ValueError if someone sets it to a non-integer string in .env. Consider wrapping in a try/except with a fallback. Not a blocker.

Thank you for the suggestion. I’ve added a try/except with a fallback to the default value.

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.

enhancement: Add query length guard in handle_chat

2 participants