#11419·prefect

JSON Serializer encoder/decoder does not handle exceptions with parameters outside of "message"

Author: robfreedyCreated Dec 14, 2023Updated Sep 30, 2026
Labelsbug

First check

  • I added a descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Prefect documentation for this issue.
  • I checked that this issue is related to Prefect and not one of its dependencies.

Bug summary

In a parent-child flow setup, if the child flow raises an exception and that exception constructor has required parameters other than the "message" parameter, then the parent flow cannot serialize the result from the child flow. This is because the decoder in the JSON serializer attempts to instantiate a new object of the exception type with only the "message" parameter and the exception type requires other arguments (see the two lines of code linked below).

In the below example, the exception raised (ClientConnectorError) requires the os_error parameter to be passed on instantiation while only message is passed into the new object, so the .result(fetch-True) fails.

Reproduction

python3
from prefect import flow
import aiohttp


@flow(persist_result=True, result_serializer="json", cache_result_in_memory=False)
async def child():
    async with aiohttp.request("GET", "https://127.0.0.1:12345"):
        pass


@flow
def parent():
    child_state = child(return_state=True)
    child_state.result(fetch=True)


def main():
    parent()


if __name__ == "__main__":
    main()

Error

python3
10:50:07.771 | INFO    | prefect.engine - Created flow run 'acrid-marten' for flow 'parent'
10:50:07.773 | INFO    | Flow run 'acrid-marten' - View at https://app.prefect.cloud/account/7a9eace3-e007-4a4e-a609-880835234645/workspace/835013e9-59aa-453b-a0e6-19c03f6dbf35/flow-runs/flow-run/46569c99-e0e2-4952-848c-b8dfd1c7df53
10:50:09.069 | INFO    | Flow run 'acrid-marten' - Created subflow run 'competent-goldfish' for flow 'child'
10:50:09.071 | INFO    | Flow run 'competent-goldfish' - View at https://app.prefect.cloud/account/7a9eace3-e007-4a4e-a609-880835234645/workspace/835013e9-59aa-453b-a0e6-19c03f6dbf35/flow-runs/flow-run/c930c848-1c5f-44dc-8068-525e0347222e
10:50:10.186 | ERROR   | Flow run 'competent-goldfish' - Encountered exception during execution:
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/connector.py", line 986, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore[return-value]  # noqa
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection
    raise exceptions[0]
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection
    sock = await self._connect_sock(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock
    await self.sock_connect(sock, address)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/asyncio/selector_events.py", line 499, in sock_connect
    return await fut
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/asyncio/selector_events.py", line 534, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 61] Connect call failed ('127.0.0.1', 12345)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/engine.py", line 844, in orchestrate_flow_run
    result = await flow_call.aresult()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 293, in aresult
    return await asyncio.wrap_future(self.future)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 355, in _run_async
    result = await coro
  File "/Users/rob.freedy/Documents/prefect/scratch/repro/flow.py", line 7, in child
    async with aiohttp.request("GET", "https://127.0.0.1:12345"):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/client.py", line 1186, in __aenter__
    self._resp = await self._coro
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/client.py", line 535, in _request
    conn = await self._connector.connect(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/connector.py", line 542, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/connector.py", line 907, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection
    raise last_exc
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/aiohttp/connector.py", line 992, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 127.0.0.1:12345 ssl:default [Connect call failed ('127.0.0.1', 12345)]
10:50:10.498 | ERROR   | Flow run 'competent-goldfish' - Finished in state Failed("Flow run encountered an exception. ClientConnectorError: Cannot connect to host 127.0.0.1:12345 ssl:default [Connect call failed ('127.0.0.1', 12345)]")
10:50:12.287 | ERROR   | Flow run 'acrid-marten' - Encountered exception during execution:
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/engine.py", line 844, in orchestrate_flow_run
    result = await flow_call.aresult()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 293, in aresult
    return await asyncio.wrap_future(self.future)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 318, in _run_sync
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/rob.freedy/Documents/prefect/scratch/repro/flow.py", line 14, in parent
    child_state.result(fetch=True)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/client/schemas/objects.py", line 206, in result
    return get_state_result(self, raise_on_failure=raise_on_failure, fetch=fetch)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/states.py", line 71, in get_state_result
    return _get_state_result(state, raise_on_failure=raise_on_failure)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/utilities/asyncutils.py", line 255, in coroutine_wrapper
    return call()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 398, in __call__
    return self.result()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 284, in result
    return self.future.result(timeout=timeout)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 168, in result
    return self.__get_result()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 355, in _run_async
    result = await coro
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/states.py", line 91, in _get_state_result
    raise await get_state_exception(state)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/states.py", line 345, in get_state_exception
    result = await state.data.get()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/client/utilities.py", line 51, in with_injected_client
    return await fn(*args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/results.py", line 515, in get
    obj = blob.serializer.loads(blob.data)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/serializers.py", line 233, in loads
    return json.loads(blob.decode(), **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/serializers.py", line 62, in prefect_json_object_decoder
    return from_qualified_name(result["__exc_type__"])(result["message"])
TypeError: ClientConnectorError.__init__() missing 1 required positional argument: 'os_error'
10:50:12.464 | ERROR   | Flow run 'acrid-marten' - Finished in state Failed("Flow run encountered an exception. TypeError: ClientConnectorError.__init__() missing 1 required positional argument: 'os_error'")
Traceback (most recent call last):
  File "/Users/rob.freedy/Documents/prefect/scratch/repro/flow.py", line 22, in <module>
    main()
  File "/Users/rob.freedy/Documents/prefect/scratch/repro/flow.py", line 18, in main
    parent()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/flows.py", line 1097, in __call__
    return enter_flow_run_engine_from_flow_call(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/engine.py", line 286, in enter_flow_run_engine_from_flow_call
    retval = from_sync.wait_for_call_in_loop_thread(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/api.py", line 243, in wait_for_call_in_loop_thread
    return call.result()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 284, in result
    return self.future.result(timeout=timeout)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 168, in result
    return self.__get_result()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 355, in _run_async
    result = await coro
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/client/utilities.py", line 51, in with_injected_client
    return await fn(*args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/engine.py", line 389, in create_then_begin_flow_run
    return await state.result(fetch=True)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/states.py", line 91, in _get_state_result
    raise await get_state_exception(state)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/engine.py", line 844, in orchestrate_flow_run
    result = await flow_call.aresult()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 293, in aresult
    return await asyncio.wrap_future(self.future)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 318, in _run_sync
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/rob.freedy/Documents/prefect/scratch/repro/flow.py", line 14, in parent
    child_state.result(fetch=True)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/client/schemas/objects.py", line 206, in result
    return get_state_result(self, raise_on_failure=raise_on_failure, fetch=fetch)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/states.py", line 71, in get_state_result
    return _get_state_result(state, raise_on_failure=raise_on_failure)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/utilities/asyncutils.py", line 255, in coroutine_wrapper
    return call()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 398, in __call__
    return self.result()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 284, in result
    return self.future.result(timeout=timeout)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 168, in result
    return self.__get_result()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 355, in _run_async
    result = await coro
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/states.py", line 91, in _get_state_result
    raise await get_state_exception(state)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/states.py", line 345, in get_state_exception
    result = await state.data.get()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/client/utilities.py", line 51, in with_injected_client
    return await fn(*args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/results.py", line 515, in get
    obj = blob.serializer.loads(blob.data)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/serializers.py", line 233, in loads
    return json.loads(blob.decode(), **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/prefect2/lib/python3.10/site-packages/prefect/serializers.py", line 62, in prefect_json_object_decoder
    return from_qualified_name(result["__exc_type__"])(result["message"])
TypeError: ClientConnectorError.__init__() missing 1 required positional argument: 'os_error'

Versions

Version:             2.14.10
API version:         0.8.4
Python version:      3.10.4
Git commit:          70888afe
Built:               Thu, Dec 7, 2023 7:27 PM
OS/Arch:             darwin/arm64
Profile:             default
Server type:         cloud

Additional context

JSON serializer encoder: https://github.com/PrefectHQ/prefect/blob/e95b0b96d4030fcc54c0971f1832e828265193b2/src/prefect/serializers.py#L43-L44

JSON serializer decoder: https://github.com/PrefectHQ/prefect/blob/e95b0b96d4030fcc54c0971f1832e828265193b2/src/prefect/serializers.py#L61-L62