Skip to content

feat: Enhance structured output for OpenAI-compatible models #3194

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

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

Conversation

riteshkhamitkar
Copy link

Closes #3174

🎯 Summary

This pull request addresses the feature request to improve structured output capabilities for a wider range of Large Language Models, particularly those with OpenAI-compatible APIs. It introduces two key enhancements:

  1. Relaxed response_format Validation: The validation for structured output (response_format) is now bypassed for any model designated as openai-compatible (e.g., openai/qwen-plus). This allows developers to leverage structured outputs with custom endpoints and third-party models without encountering ValueError.
  2. Self-Healing JSON Parsing: A robust, multi-step JSON parsing mechanism has been implemented in converter.py. This significantly reduces application crashes caused by minor LLM formatting errors (e.g., extra text, missing brackets) by intelligently extracting and repairing the JSON object from the raw output.

✨ Key Changes

1. src/crewai/llm.py

  • The _validate_call_params method has been updated to skip the supports_response_schema check when the model's provider is openai or None (which defaults to an OpenAI-compatible interface).
  • This delegates the responsibility of handling response_format to the target API endpoint, which is the correct behavior for compatible models.
  • Validation for other providers like anthropic, gemini, etc., remains unchanged, ensuring no existing functionality is broken.

2. src/crewai/utilities/converter.py

  • A new function, handle_partial_json, has been introduced to act as a "self-healing" mechanism for malformed JSON.
  • The convert_to_model function now follows a more resilient parsing flow:
    1. Attempt Direct Parse: Tries to parse the raw output as valid JSON.
    2. Attempt Regex Extraction: If direct parsing fails, it uses a regular expression to find and extract a JSON object from within the text (e.g., stripping "Here is the JSON: ...").
    3. Fallback to LLM Correction: If both attempts fail, it falls back to the original method of re-prompting the LLM to correct the format.

✅ Benefits

  • Broader Model Compatibility: Developers can now confidently use output_pydantic and output_json with any OpenAI-compatible model.
  • Increased Reliability: The self-healing JSON parser drastically reduces runtime errors, making agentic workflows more stable and production-ready.
  • Improved Developer Experience: Eliminates the need for developers to write their own complex and brittle parsing logic.

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.

[FEATURE] We need structured output capabilities that can be compatible with more models.
1 participant