3
3
import logging
4
4
import json
5
5
import time
6
+ from datetime import datetime
6
7
from aimon import Client , APIStatusError
7
8
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
+
8
29
class TestLowLevelAPIWithRealService :
9
30
"""Test the low-level API client functions with the real AIMon service."""
10
31
@@ -386,9 +407,9 @@ def test_evaluation_run_create(self):
386
407
collection = self .client .datasets .collection .create (name = self .collection_name , dataset_ids = [dataset .sha ], description = f"Prereq collection { self .timestamp } " )
387
408
evaluation = self .client .evaluations .create (
388
409
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
392
413
)
393
414
self .log_info ("Prerequisites created" , {"evaluation" : evaluation .id })
394
415
except Exception as e :
@@ -397,12 +418,28 @@ def test_evaluation_run_create(self):
397
418
# Create Evaluation Run
398
419
try :
399
420
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
400
427
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
403
432
)
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
+
404
441
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
406
443
assert create_response .id is not None
407
444
self .log_info ("Create Run Response. metrics config?" , create_response )
408
445
# assert 'hallucination' in create_response.metrics_config
0 commit comments