Skip to content

[Bug]: datetime serialization for InvocationCompletedDetails breaks sam-cli #193

@yaythomas

Description

@yaythomas

Expected Behavior

  1. Lambda function invocation completes successfully
  2. Execution state (including invocation completion timestamps) is serialized to JSON
  3. Execution is saved to SQLite storage without errors
  4. Test execution completes successfully

Actual Behavior

  1. Lambda function invocation completes successfully ✅
  2. Execution state serialization fails with TypeError
  3. Error message: Invalid execution data: Object of type datetime is not JSON serializable
  4. Test execution fails

Steps to Reproduce

Run from sam cli invoke to repro.

SDK Version

1.1.1

Python Version

3.14

Is this a regression?

No

Last Working Version

No response

Additional Context

The Execution.to_json_dict() method calls completion.to_dict() for InvocationCompletedDetails objects, which returns raw datetime objects instead of JSON-serializable Unix millisecond timestamps.

Problematic code in execution.py (line 109-110):

"InvocationCompletions": [
    completion.to_dict() for completion in self.invocation_completions  # ← BUG
],

Problematic code in model.py (line 1242-1247):

def to_dict(self) -> dict[str, Any]:
    return {
        "StartTimestamp": self.start_timestamp,  # ← datetime object, not JSON-serializable
        "EndTimestamp": self.end_timestamp,      # ← datetime object, not JSON-serializable
        "RequestId": self.request_id,
    }

When SQLite storage attempts to serialize the execution:

json.dumps(execution.to_json_dict())  # Fails because datetime objects are present

The web API layer already handles datetime serialization correctly using a custom JSON encoder, but the storage layer uses standard json.dumps() without custom encoding. This fix ensures that all data passed to json.dumps() is already in JSON-serializable format (Unix milliseconds as integers) rather than relying on custom encoders.

This pattern matches the existing SDK design where Operation objects have both:

to_dict() - returns datetime objects for internal use
to_json_dict() - returns Unix milliseconds for JSON serialization

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions