feat: add model serialization with input types and constraint types#103
feat: add model serialization with input types and constraint types#103Kamilbenkirane merged 2 commits intomainfrom
Conversation
- Add `type` computed_field to Constraint for JSON serialization - Add `InputType` enum (text, image, video, audio) to core - Add `supported_input_types` computed field to Model (per-capability) - Add `optional_input_types` computed field to Model (from constraints) - Add `get_required_input_types()` and `get_constraint_input_type()` helpers - Use `SerializeAsAny[Constraint]` for proper polymorphic serialization This enables the API to return complete model metadata including: - What input types each capability requires (e.g., text-generation: text) - What optional inputs are accepted via parameters (e.g., reference_images) - Constraint type identifiers for frontend rendering
Pull Request Review: Model Serialization with Input Types and Constraint TypesSummaryThis PR adds computed fields to Model and Constraint classes to enable proper JSON serialization with metadata about input types and constraint types. The implementation is well-designed and follows good software engineering practices. Code Quality and Best PracticesStrengths
Areas for Improvement1. Missing Tests (Priority: HIGH)The PR adds significant new functionality but I don't see corresponding test coverage. Missing test cases include:
Recommendation: Add a new test file tests/unit_tests/test_io.py or expand test_models.py with tests for the new computed fields and helper functions. 2. Potential Edge Cases in _extract_input_type (Priority: MEDIUM)In io.py:95-116, the condition 'if origin is types.UnionType or origin is not None:' means ANY generic type (not just unions) will trigger recursion into args. While this works, it could lead to unexpected behavior with complex nested types. Recommendation: Be more explicit about which types should be recursively examined. 3. Documentation (Priority: LOW)The computed fields have docstrings, but they're quite brief. Consider adding examples showing what the return values look like. 4. Error Handling (Priority: LOW)In get_required_input_types (io.py:75-92), if get_input_class raises a KeyError, the error will propagate. Consider whether this is the desired behavior or if it should return an empty set for unregistered capabilities. Potential Bugs or IssuesIssue 1: KeyError for Unregistered CapabilitiesLocation: io.py:87, models.py:31 If a Model has a capability that hasn't had an Input class registered yet (e.g., during initialization), calling model.supported_input_types will raise a KeyError. Scenario: A model with Capability.SEARCH (if no Input registered for SEARCH) will raise KeyError when accessing supported_input_types. Recommendation: Add error handling in supported_input_types computed field to gracefully skip capabilities without registered Input classes, or document this requirement clearly. Issue 2: Return Type InconsistencyLocation: io.py:119-136 The function get_constraint_input_type returns InputType | None. The function could return early on first match for better readability rather than continuing to iterate through all annotations. Performance ConsiderationsGood Choices
Minor OptimizationThe optional_input_types property (models.py:35-42) iterates through all constraints. For models with many constraints, this is acceptable but worth noting. Security ConcernsNo Major Security Issues
Test CoverageCritical Gap: Missing Test CoverageBased on my review of the test files, there are NO tests for the new functionality. Required tests include:
Overall AssessmentThis is a well-designed feature that follows good architectural principles. The code quality is high, and the implementation is clean. However, the PR should not be merged without addressing the test coverage gap. Recommendations Before Merge:
Score: 7.5/10
Great work on the implementation! Once tests are added, this will be a solid contribution. |
Code Review - PR #103: Add Model Serialization with Input Types and Constraint TypesSummaryThis PR adds computed fields to enable complete model metadata serialization, including input type requirements and constraint type identifiers. The implementation is well-structured and follows good architectural patterns. ✅ Strengths
|
🔍 Code Quality Issues1. Missing Test Coverage
|
2. Potential Runtime Error in
|
3. Incomplete Type Handling in
|
🔒 Security ConsiderationsNo security issues identified. The code:
⚡ Performance ConsiderationsComputed Fields May Be Expensive ℹ️ INFORMATIONALLocation: The computed fields call @computed_field
@property
def supported_input_types(self) -> dict[Capability, set[InputType]]:
return {cap: get_required_input_types(cap) for cap in self.capabilities} # Not cachedImpact:
Recommendation (future optimization): Note: This is not blocking for the current PR, but worth considering if serialization becomes a bottleneck. |
📝 Best PracticesGood:
Could Improve:
📋 RecommendationsBefore Merging (Required):
Future Improvements (Optional):
🎯 VerdictConditional Approval - The architecture and implementation are solid, but test coverage is critical before merging. The potential KeyError in Once tests are added and the union type handling is fixed, this will be a valuable addition to the API. Great work on the overall design! The computed field approach provides a clean, discoverable API for consumers. |
Summary
typecomputed_field to Constraint for JSON serializationInputTypeenum (text, image, video, audio) to coresupported_input_typescomputed field to Model (per-capability)optional_input_typescomputed field to Model (from constraints)get_required_input_types()andget_constraint_input_type()helpersSerializeAsAny[Constraint]for proper polymorphic serializationThis enables the API to return complete model metadata including:
Test plan