8000 fix: add LLM schema preparation utilities and enhance schema processi… by tonmanna · Pull Request #327 · presenton/presenton · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@tonmanna
Copy link
@tonmanna tonmanna commented Oct 20, 2025

Fix: Resolve Google Gemini "schema exceeds maximum nesting depth" error

Problem

Google Gemini API was rejecting structured output requests with a schema validation error:

google.genai.errors.ClientError: 400 INVALID_ARGUMENT
'A schema in GenerationConfig in the request exceeds the maximum allowed nesting depth.'

This occurred because our presentation schemas had deeply nested structures that exceeded Google Gemini's limits:

  • Max nesting depth: ~5 levels
  • Schema complexity: Constraints like minLength, maxLength, minItems, maxItems create "too many states"

Solution

Implemented a schema processing pipeline that:

  1. Flattens $ref references to reduce indirection
  2. Removes metadata fields (e.g., title)
  3. Strips problematic constraints:
    • String: minLength, maxLength, pattern, format
    • Array: minItems, maxItems
    • Number: minimum, maximum
    • Other: enum, const, default, examples
  4. Limits nesting depth to provider-specific maximum

Changes

New Files

  • servers/fastapi/utils/llm_utils.py - Central utility for LLM schema preparation with provider-specific helpers:
    • prepare_schema_for_google() (max_depth=4)
    • prepare_schema_for_openai() (max_depth=5)
    • prepare_schema_for_anthropic() (max_depth=5)

Modified Files

  • servers/fastapi/utils/schema_utils.py - Added schema simplification functions
  • servers/fastapi/services/llm_client.py - Applied schema processing to all providers
  • servers/fastapi/api/v1/ppt/endpoints/presentation.py - Fixed unawaited coroutine warnings

Provider Settings

Provider Max Depth Remove Constraints Debug Logging
Google 4 ✅ Yes ❌ No
OpenAI 5 ✅ Yes ❌ No
Anthropic 5 ✅ Yes ❌ No

Result

Schemas are now automatically processed before being sent to LLM providers, preventing validation errors while maintaining the necessary structure for structured output generation.

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.

1 participant

0