33import logging
44import json
55import time
6+ from datetime import datetime
67from aimon import Client , APIStatusError
78
9+ def parse_datetime (dt_str ):
10+ """Parse datetime string in various formats to ensure compatibility with Pydantic."""
11+ if not dt_str or not isinstance (dt_str , str ):
12+ return dt_str
13+ try :
14+ # Try to parse RFC 1123 format (e.g., 'Mon, 28 Apr 2025 19:40:50 GMT')
15+ formats = [
16+ '%a, %d %b %Y %H:%M:%S GMT' , # RFC 1123 format
17+ '%Y-%m-%dT%H:%M:%S.%fZ' , # ISO 8601 format
18+ '%Y-%m-%dT%H:%M:%SZ' , # ISO 8601 without microseconds
19+ ]
20+ for fmt in formats :
21+ try :
22+ return datetime .strptime (dt_str , fmt )
23+ except ValueError :
24+ continue
25+ return dt_str # Return original if parsing fails
26+ except Exception :
27+ return dt_str # Return original on any error
28+
829class TestLowLevelAPIWithRealService :
930 """Test the low-level API client functions with the real AIMon service."""
1031
@@ -386,9 +407,9 @@ def test_evaluation_run_create(self):
386407 collection = self .client .datasets .collection .create (name = self .collection_name , dataset_ids = [dataset .sha ], description = f"Prereq collection { self .timestamp } " )
387408 evaluation = self .client .evaluations .create (
388409 name = self .evaluation_name ,
389- application_id = app .id ,
390- model_id = model .id ,
391- dataset_collection_id = collection .id
410+ application_id = str ( app .id ), # Convert to string to avoid Pydantic warnings
411+ model_id = str ( model .id ), # Convert to string to avoid Pydantic warnings
412+ dataset_collection_id = str ( collection .id ) # Convert to string to avoid Pydantic warnings
392413 )
393414 self .log_info ("Prerequisites created" , {"evaluation" : evaluation .id })
394415 except Exception as e :
@@ -397,12 +418,28 @@ def test_evaluation_run_create(self):
397418 # Create Evaluation Run
398419 try :
399420 metrics_config = {'hallucination' : {'detector_name' : 'default' }, 'toxicity' : {'detector_name' : 'default' }}
421+
422+ # Handle creation_time and completed_time if they come as string responses
423+ creation_time = None
424+ completed_time = None
425+
426+ # When creating an evaluation run, use the helper to handle date strings
400427 create_response = self .client .evaluations .run .create (
401- evaluation_id = evaluation .id ,
402- metrics_config = metrics_config
428+ evaluation_id = str (evaluation .id ), # Convert to string to avoid Pydantic warnings
429+ metrics_config = metrics_config ,
430+ creation_time = creation_time ,
431+ completed_time = completed_time
403432 )
433+
434+ # Post-process the response if needed
435+ if hasattr (create_response , 'creation_time' ) and isinstance (create_response .creation_time , str ):
436+ create_response .creation_time = parse_datetime (create_response .creation_time )
437+
438+ if hasattr (create_response , 'completed_time' ) and isinstance (create_response .completed_time , str ):
439+ create_response .completed_time = parse_datetime (create_response .completed_time )
440+
404441 self .log_info ("Create Run Response" , create_response .model_dump ())
405- assert create_response .evaluation_id == evaluation .id
442+ assert create_response .evaluation_id == str ( evaluation .id ) # Convert to string for comparison
406443 assert create_response .id is not None
407444 self .log_info ("Create Run Response. metrics config?" , create_response )
408445 # assert 'hallucination' in create_response.metrics_config
0 commit comments