@@ -378,17 +378,95 @@ def _get_stickler_model(
378378 schema = stickler_config ["schema" ]
379379 model_name = stickler_config ["model_name" ]
380380
381- logger .info (f"Creating Stickler model for class: { document_class } " )
382-
383- # Use JsonSchemaFieldConverter to handle the full JSON Schema natively
384- from stickler .structured_object_evaluator .models .json_schema_field_converter import (
385- JsonSchemaFieldConverter ,
381+ # Enhanced logging: Log schema details before creating model
382+ logger .info (
383+ f"Creating Stickler model for class: { document_class } \n "
384+ f" Schema summary:\n "
385+ f" - Properties: { list (schema .get ('properties' , {}).keys ())} \n "
386+ f" - Required fields: { schema .get ('required' , [])} \n "
387+ f" - Schema ID: { schema .get ('$id' , 'N/A' )} \n "
388+ f" - Model name: { model_name } "
386389 )
387390
388- converter = JsonSchemaFieldConverter (schema )
389- field_definitions = converter .convert_properties_to_fields (
390- schema .get ("properties" , {}), schema .get ("required" , [])
391- )
391+ # Log expected and actual data structure for troubleshooting
392+ if expected_data :
393+ logger .info (
394+ f" Expected data keys for { document_class } : { list (expected_data .keys ())} "
395+ )
396+
397+ try :
398+ # Use JsonSchemaFieldConverter to handle the full JSON Schema natively
399+ from stickler .structured_object_evaluator .models .json_schema_field_converter import (
400+ JsonSchemaFieldConverter ,
401+ )
402+
403+ logger .debug (f"Converting schema properties for { document_class } " )
404+
405+ converter = JsonSchemaFieldConverter (schema )
406+ field_definitions = converter .convert_properties_to_fields (
407+ schema .get ("properties" , {}), schema .get ("required" , [])
408+ )
409+
410+ logger .info (
411+ f"Successfully converted schema for { document_class } with { len (field_definitions )} fields"
412+ )
413+
414+ except Exception as e :
415+ # Enhanced error handling with user guidance
416+ import json
417+ import re
418+
419+ error_message = str (e )
420+
421+ # Check if it's a JSON Schema validation error
422+ if (
423+ "jsonschema.exceptions.SchemaError" in str (type (e ))
424+ or "Invalid JSON Schema" in error_message
425+ ):
426+ # Try to extract the problematic field from the error
427+ field_match = re .search (
428+ r"On schema\['properties'\]\['([^']+)'\]" , error_message
429+ )
430+ field_name = field_match .group (1 ) if field_match else "unknown"
431+
432+ # Parse for constraint information
433+ constraint_match = re .search (
434+ r"\['([^']+)'\]\s*:\s*'([^']+)'" , error_message
435+ )
436+ constraint = (
437+ constraint_match .group (1 ) if constraint_match else "unknown"
438+ )
439+ bad_value = constraint_match .group (2 ) if constraint_match else "unknown"
440+
441+ # Build helpful error message
442+ helpful_message = (
443+ f"Invalid JSON Schema for document class '{ document_class } '.\n \n "
444+ f"Problem detected:\n "
445+ f" Field: { field_name } \n "
446+ f" Constraint: { constraint } \n "
447+ f" Current value: '{ bad_value } ' (type: { type (bad_value ).__name__ } )\n \n "
448+ f"Common fixes:\n "
449+ f" 1. If '{ constraint } ' should be a number, remove quotes in your config:\n "
450+ f" { constraint } : '{ bad_value } ' → { constraint } : { bad_value } \n "
451+ f" 2. Check your config YAML for field '{ field_name } ' in class '{ document_class } '\n "
452+ f" 3. Ensure all numeric constraints (maxItems, minItems, minimum, maximum, etc.) are numbers, not strings\n \n "
453+ f"Original error: { error_message } "
454+ )
455+
456+ logger .error (helpful_message )
457+ logger .error (
458+ f"Full schema that caused the error:\n { json .dumps (schema , indent = 2 , default = str )} "
459+ )
460+ raise ValueError (helpful_message ) from e
461+ else :
462+ # Re-raise other errors with schema details
463+ logger .error (
464+ f"Unexpected error creating Stickler model for { document_class } : { error_message } "
465+ )
466+ logger .error (
467+ f"Schema being processed:\n { json .dumps (schema , indent = 2 , default = str )} "
468+ )
469+ raise
392470
393471 # Create the model using Pydantic's create_model
394472 from pydantic import create_model
0 commit comments