Skip to content

fix: add tool count validation with env-configurable MAX_TOOLS - #3

Closed
lireno wants to merge 1 commit into
mainfrom
fix/tool-count-validation
Closed

fix: add tool count validation with env-configurable MAX_TOOLS#3
lireno wants to merge 1 commit into
mainfrom
fix/tool-count-validation

Conversation

@lireno

@lireno lireno commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes aipotheosis-labs#337 — prevents cryptic OpenAI API errors when too many functions are selected (e.g., enabling all Gmail functions in the playground).

This is a cleaner, improved version of PR aipotheosis-labs#604 that adds env-var configurability, better error messaging, and comprehensive tests.

What changed

Core changes

  • config.py: Added MAX_TOOLS = 64 default, overridable via SERVER_MAX_TOOLS env var
  • exceptions.py: New ToolCountExceeded exception with actionable 400 response message
  • routes/agent.py: Early tool-count validation in /chat route before any API call
  • agent/prompt.py: Defensive warning log in openai_chat_stream for other call-sites
  • tests/routes/agent/test_chat_tool_validation.py: 11 test cases

Improvements over PR aipotheosis-labs#604

  1. Env-var configurability: MAX_TOOLS is configurable via SERVER_MAX_TOOLS, not a hardcoded constant
  2. Better error messages: Shows actual tool count and limit, plus actionable guidance to reduce functions
  3. Comprehensive tests: 11 test cases covering exception shape, config defaults, boundary conditions (at-limit, over-limit, under-limit), custom limits, and zero-tools edge case
  4. Proper exception class: Uses ACIException subclass pattern consistent with the codebase

Why this fix works

Validates tool count before making the API call. Returns a 400 with a clear message telling users to reduce selected functions. The default limit of 64 is conservative — the actual OpenAI limit is closer to 128 or ~200k tokens of tool definitions, but this provides a safe margin.

Risk assessment

  • Low risk: validation-only change
  • No behavioral change for requests under the limit
  • Existing requests that would fail now fail earlier with a useful message
  • Backward compatible

Closes aipotheosis-labs#337

Prevents cryptic 'There was an issue with your request' errors from
OpenAI's API when too many functions are selected (e.g., enabling all
Gmail functions in the playground).

Changes:
- Add MAX_TOOLS=64 default in config.py, overridable via SERVER_MAX_TOOLS
- Add ToolCountExceeded exception with actionable 400 response
- Add early validation in /chat route before any API call is made
- Add defensive warning log in openai_chat_stream for other call-sites
- Add comprehensive tests for exception, config, and /chat endpoint

Closes aipotheosis-labs#337

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@lireno lireno closed this Jun 2, 2026
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.

AI returns empty response when multiple GMAIL functions are selected

1 participant