-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Implement LLM generations, logprobs, and XML parsing features #3053
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
Implement LLM generations, logprobs, and XML parsing features #3053
Conversation
- Add support for n generations and logprobs parameters in LLM class - Extend Agent class to accept LLM generation parameters (llm_n, llm_logprobs, llm_top_logprobs) - Add return_full_completion parameter to access complete LLM response metadata - Implement XML parser utility for extracting content from tags like <thinking> - Add completion metadata support to TaskOutput and LiteAgentOutput classes - Add comprehensive tests and examples demonstrating new functionality - Maintain full backward compatibility with existing code Addresses issue #3052: How to obtain n generations or generations in different tags Co-Authored-By: João <joao@crewai.com>
Disclaimer: This review was made by a crew of AI Agents. Code Review CommentOverviewThe recent pull request implements significant enhancements for handling multiple LLM generations, tracking log probabilities, and parsing structured XML content. The changes reflect a substantial upgrade across core components and include increased test coverage. Key Components Analysis1. LLM Class Enhancements
2. XML Parser Implementation
3. Agent Class Updates
4. Task Output Enhancement
Testing Coverage Analysis
Performance Considerations
Security Recommendations
Documentation Recommendations
In summary, while the implementation shows great promise, it requires focused improvements in security, validation, and performance optimization to ensure reliability and usability moving forward. |
Disclaimer: This review was made by a crew of AI Agents. Code Review for PR #3053: Implement LLM Generations, Logprobs, and XML Parsing FeaturesSummary of Key FindingsThis PR introduces significant, well-integrated features to enhance the LLM handling capabilities and output processing in crewAI:
Detailed Review and Improvement Suggestions by Component1. Examples (
|
Area | Issue / Suggestion | Suggested Improvement |
---|---|---|
agent.py |
Repetitive param assignment and lack of validation | Refactor looping assignments and add validation |
llm.py |
Code duplication constructing completion dict | Extract helper method for dict construction |
lite_agent.py , task_output.py |
Duplicate metadata extraction methods | Extract shared utilities, normalize choice data |
agent_utils.py |
Repeated error checking | Consolidate error handling and log selectively |
xml_parser.py |
Limited malformed tag handling, no nested support | Document limitations, consider adding error warnings |
Tests | Repeated mocks, missing negative cases | Use fixtures, add edge case tests and docstrings |
Examples | Code duplication in print, missing type hints | Helper functions, add type hints |
Final Remarks
This PR is a high-quality, impactful enhancement that meaningfully expands the crewAI LLM integration and output utility. It carefully preserves legacy behavior while enabling advanced use cases such as multiple generations, confidence metrics via logprobs, and easily parsed structured reasoning via XML tags.
Addressing the above noted improvements around DRY principles, validation, and documentation will improve maintainability and robustness. The comprehensive test coverage demonstrates attention to quality.
Thank you for considering these points; happy to discuss or help with any follow-up refinements!
- Remove unused imports from test and example files - Fix f-string formatting in examples - Add proper type handling for Union[str, Dict] in agent_utils.py - Fix XML parser test assertion to match expected output - Use isinstance() for proper type narrowing in LLM calls Co-Authored-By: João <joao@crewai.com>
- Fix type checker errors in reasoning_handler.py: handle Union[str, dict] response types - Fix type checker error in crew_chat.py: convert final_response to string for dict - Update test_task_callback_returns_task_output to include completion_metadata field - Fix integration test attribute access in test_lite_agent_with_xml_extraction Co-Authored-By: João <joao@crewai.com>
- Fix test_crew_with_llm_parameters: mock _run_sequential_process instead of kickoff to avoid circular mocking - Fix test_lite_agent_with_xml_extraction: access result.raw instead of result.output for LiteAgentOutput Co-Authored-By: João <joao@crewai.com>
…string - Mock _invoke_loop to return proper AgentFinish object with output attribute - This should resolve the 'str' object has no attribute 'output' error in CI Co-Authored-By: João <joao@crewai.com>
Closing due to inactivity for more than 7 days. Configure here. |
Implement LLM Generations, Logprobs, and XML Parsing Features
This PR implements the feature request from issue #3052, adding support for advanced LLM parameters and XML tag parsing capabilities to the CrewAI framework.
🚀 Features Added
1. LLM Multiple Generations Support
n
parameter support to generate multiple completionslogprobs
andtop_logprobs
parameters for accessing log probabilitiesreturn_full_completion
parameter to access complete response metadata2. Agent-Level LLM Parameter Control
llm_n
,llm_logprobs
,llm_top_logprobs
return_completion_metadata
parameter for accessing generation metadata3. XML Content Extraction Utility
xml_parser.py
utility for extracting content from XML tags<thinking>
,<reasoning>
,<conclusion>
and other custom tags4. Enhanced Output Classes
TaskOutput
with completion metadata and helper methodsLiteAgentOutput
with completion metadata supportget_generations()
,get_logprobs()
,get_usage_metrics()
📝 Usage Examples
Multiple Generations
XML Tag Extraction
Log Probabilities
🧪 Testing
🔄 Backward Compatibility
All changes are fully backward compatible. Existing code will continue to work exactly as before. The new functionality is opt-in through new parameters and methods.
📁 Files Changed
Core Implementation
src/crewai/llm.py
- Enhanced LLM class with new parameters and completion metadatasrc/crewai/agent.py
- Added LLM parameter supportsrc/crewai/lite_agent.py
- Added completion metadata supportsrc/crewai/tasks/task_output.py
- Enhanced with metadata and helper methodssrc/crewai/utilities/agent_utils.py
- Updated to handle completion metadataNew Utilities
src/crewai/utilities/xml_parser.py
- XML content extraction utilityTests and Examples
tests/test_llm_generations_logprobs.py
- Core functionality teststests/test_integration_llm_features.py
- Integration teststests/test_xml_parser_examples.py
- XML parser testsexamples/llm_generations_example.py
- Usage examples🔗 Related
✅ Verification
The implementation has been tested with:
All new features work as expected while maintaining full backward compatibility with existing CrewAI applications.