pythonboto3amazon-sagemaker

sagemaker list_training_jobs not returning all completed jobs


I wrote this function

def list_completed_training_jobs():
    """Lists completed SageMaker training jobs."""

    sagemaker = boto3.client('sagemaker',  region_name="us-east-1")

    response = sagemaker.list_training_jobs(
        StatusEquals='Completed',
        CreationTimeAfter=datetime(2023, 1, 1),
    )

    training_jobs = response['TrainingJobSummaries']

    while 'NextToken' in response:
        response = sagemaker.list_training_jobs(
            StatusEquals='Completed',
            NextToken=response['NextToken']
        )
        training_jobs.extend(response['TrainingJobSummaries'])

    return training_jobs

I am trying to get all jobs after 2023, but I am getting this error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[71], line 36
     27     df.to_csv('../output/training_jobs.cvs',index=False)
     30     return df
---> 36 completed_jobs = list_completed_training_jobs()
     37 # df = create_training_name_cols(completed_jobs)

Cell In[71], line 8, in list_completed_training_jobs()
      2 """Lists completed SageMaker training jobs."""
      4 sagemaker = boto3.client('sagemaker',  region_name="us-east-1")
      6 response = sagemaker.list_training_jobs(
      7     StatusEquals='Completed',
----> 8     CreationTimeAfter=datetime(2023, 1, 1),
      9 )
     11 training_jobs = response['TrainingJobSummaries']
     13 while 'NextToken' in response:

TypeError: 'module' object is not callable

Not sure what the problem is any suggestions are appreciated. Even if I remove the CreationTimeAfter parameter the code works but only returns training jobs from 2021 to 2022 but nothing for 2023 or 2024.


Solution

  • The message TypeError: 'module' object is not callable, typically occurs when you attempt to call a module as if it were a function. In this case, the problem is with the datetime module.

    Try to import the datetime class from the datetime module.

    from datetime import datetime