Skip to content

ValueError when parsing RTDB timestamps without microseconds in db_fn.py #257

@jniebur

Description

@jniebur

The firebase-functions library crashes when handling Real-Time Database (RTDB) events with timestamps that don't include microseconds. The _db_endpoint_handler function in db_fn.py at line 114 attempts to parse timestamps using a strict format string '%Y-%m-%dT%H:%M:%S.%f%z' that requires microseconds, but Firebase sometimes sends timestamps in the format '2025-10-30T21:15:51Z' without the .%f component.

Similar to this issue.

Reproducibility: intermittent

To Reproduce

  1. Set up a Firebase Function using on_value_written decorator for Real-Time Database
  2. Trigger a write event to the RTDB
  3. The function crashes when Firebase sends a timestamp without microseconds

Error Message

ValueError: time data '2025-10-30T21:15:51Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

Full Stack Trace

Exception on / [POST]
  Traceback (most recent call last):
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1511, in wsgi_app
      response = self.full_dispatch_request()
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 919, in full_dispatch_request
      rv = self.handle_user_exception(e)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 917, in full_dispatch_request
      rv = self.dispatch_request()
           ^^^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 902, in dispatch_request
      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/functions_framework/execution_id.py", line 157, in wrapper
      result = view_function(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/functions_framework/__init__.py", line 188, in view_func
      function(event)
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/firebase_functions/db_fn.py", line 154, in on_value_written_wrapped
      return _db_endpoint_handler(
             ^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/firebase_functions/db_fn.py", line 114, in _db_endpoint_handler
      time=_dt.datetime.strptime(
           ^^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.runtime/python/lib/python3.11/_strptime.py", line 567, in _strptime_datetime
      tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/layers/google.python.runtime/python/lib/python3.11/_strptime.py", line 349, in _strptime
      raise ValueError("time data %r does not match format %r" %
  ValueError: time data '2025-10-30T21:15:51Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

Environment

  • Python version: 3.11
  • firebase-functions version: 0.4.3
  • Deployment: Google Cloud Functions (2nd gen)
  • Trigger type: Real-Time Database (on_value_written)
  • Runtime: python311

Additional Context

The problematic code in db_fn.py:114 uses a single strict format string that requires microseconds. The timestamp '2025-10-30T21:15:51Z' is a valid ISO 8601 timestamp but doesn't match the expected format '%Y-%m-%dT%H:%M:%S.%f%z'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions