Title: JsonDataConverter.can_reconstruct omits dict/Mapping and tuple containers, skipping type-directed input reconstruction · Issue #344 · microsoft/durabletask-python · GitHub
Open Graph Title: JsonDataConverter.can_reconstruct omits dict/Mapping and tuple containers, skipping type-directed input reconstruction · Issue #344 · microsoft/durabletask-python
X Title: JsonDataConverter.can_reconstruct omits dict/Mapping and tuple containers, skipping type-directed input reconstruction · Issue #344 · microsoft/durabletask-python
Description: Problem Description In durabletask/serialization.py, _coerce_generic() supports typed coercion for dict / Mapping and tuple containers: https://github.com/microsoft/durabletask-python/blob/06bb27763eb893693f1bc300b86a86e01a5fa641/durable...
Open Graph Description: Problem Description In durabletask/serialization.py, _coerce_generic() supports typed coercion for dict / Mapping and tuple containers: https://github.com/microsoft/durabletask-python/blob/06bb2776...
X Description: Problem Description In durabletask/serialization.py, _coerce_generic() supports typed coercion for dict / Mapping and tuple containers: https://github.com/microsoft/durabletask-python/blob/06bb2776...
Opengraph URL: https://github.com/microsoft/durabletask-python/issues/344
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JsonDataConverter.can_reconstruct omits dict/Mapping and tuple containers, skipping type-directed input reconstruction","articleBody":"### Problem Description\n\nIn `durabletask/serialization.py`, `_coerce_generic()` supports typed coercion for `dict` / `Mapping` and `tuple` containers:\n\nhttps://github.com/microsoft/durabletask-python/blob/06bb27763eb893693f1bc300b86a86e01a5fa641/durabletask/serialization.py#L623-L645\n\n```python\n if origin in (dict, Mapping) and isinstance(value, dict):\n mapping = cast(\"dict[Any, Any]\", value)\n val_type = args[1] if len(args) == 2 else None\n if val_type is None:\n return mapping\n return {k: _coerce_to_type(v, val_type, converter) for k, v in mapping.items()}\n if origin is tuple and isinstance(value, list):\n if not args:\n return tuple(cast(list[Any], value))\n if len(args) == 2 and args[1] is Ellipsis:\n return tuple(_coerce_to_type(item, args[0], converter) for item in cast(list[Any], value))\n arr = cast(list[Any], value)\n if len(arr) != len(args):\n raise TypeError(\n f\"Could not coerce JSON array of length {len(arr)} to \"\n f\"tuple of length {len(args)}\"\n )\n return tuple(_coerce_to_type(item, t, converter) for item, t in zip(arr, args))\n```\n\nAnd the module documentation explicitly states:\n\n> Type-directed reconstruction recurses through dataclass fields, `list`/`Sequence`, `dict`/`Mapping` values, `tuple` elements, and `Optional` / `Union` hints.\n\nHowever, `_can_reconstruct()` only checks `Union`, `list`, `Sequence`, and `set`/`frozenset`:\n\nhttps://github.com/microsoft/durabletask-python/blob/06bb27763eb893693f1bc300b86a86e01a5fa641/durabletask/serialization.py#L280-L295\n\n```python\ndef _can_reconstruct(converter: DataConverter, target_type: Any) -> bool:\n origin = typing.get_origin(target_type)\n if origin is not None:\n args = typing.get_args(target_type)\n if origin is typing.Union or origin is types.UnionType:\n return any(\n converter.can_reconstruct(a) for a in args if a is not type(None)\n )\n if origin in (list, Sequence):\n return any(converter.can_reconstruct(a) for a in args)\n if origin in (set, frozenset, AbstractSet, typing.AbstractSet):\n if not getattr(converter, \"enable_generic_sets\", False):\n return False\n if not args:\n return True\n return _is_supported_set_element(converter, args[0])\n return False\n```\n\nIt completely omits `dict`, `Mapping`, and `tuple`.\n\nBecause `converter.can_reconstruct(dict[str, MyDataclass])` and `converter.can_reconstruct(tuple[MyDataclass, ...])` return `False`:\nIn `durabletask/internal/type_discovery.py`:\n\n```python\ndef activity_input_type(fn: Callable[..., Any], converter: DataConverter | None = None) -> type | None:\n ...\n return annotation if _resolve_converter(converter).can_reconstruct(annotation) else None\n```\n\nInbound type discovery drops the annotation and resolves the target type to `None`. When the worker executes the activity or orchestrator:\n\n```python\ninput_type = type_discovery.activity_input_type(fn, self._data_converter) if encoded_input else None\nactivity_input = self._data_converter.deserialize(encoded_input, input_type)\nactivity_output = fn(ctx, activity_input)\n```\n\nBecause `input_type` is `None`, the activity receives raw JSON dicts / lists instead of typed dataclass or model instances, resulting in runtime errors such as `AttributeError: 'dict' object has no attribute '...'`.\n\n---\n\n### Minimal Reproduction\n\n```python\nimport json\nfrom dataclasses import dataclass\nfrom durabletask import worker\nfrom durabletask.serialization import JsonDataConverter\nfrom durabletask.internal import type_discovery\n\n@dataclass\nclass User:\n name: str\n\ndef process_users_activity(ctx, users: dict[str, User]):\n for k, u in users.items():\n assert isinstance(u, User), f\"Expected User for key {k}, got {type(u)}\"\n return len(users)\n\nconverter = JsonDataConverter()\n\n# 1. Type discovery returns None:\ndiscovered_type = type_discovery.activity_input_type(process_users_activity, converter)\nprint(\"Discovered input type:\", discovered_type)\nassert discovered_type is None # BUG: Expected dict[str, User]\n\n# 2. Activity invocation fails:\nreg = worker._Registry()\nact_name = reg.add_activity(process_users_activity)\nw = worker._ActivityExecutor(reg, worker.logging.getLogger(), converter)\npayload = json.dumps({\"u1\": {\"name\": \"Alice\"}})\n\ntry:\n w.execute(\"orch-1\", act_name, 1, payload)\nexcept AssertionError as e:\n print(\"Activity execution failed:\", e)\n```\n\nOutput:\n```\nDiscovered input type: None\nActivity execution failed: Expected User for key u1, got \u003cclass 'dict'>\n```\n\n---\n\n### Expected Behavior\n\n1. `JsonDataConverter.can_reconstruct(dict[K, V])` and `can_reconstruct(Mapping[K, V])` should return `True` when `V` is reconstructable.\n2. `JsonDataConverter.can_reconstruct(tuple[T, ...])` and `can_reconstruct(tuple[T1, T2, ...])` should return `True` when the element type(s) are reconstructable.\n3. Inbound type discovery should preserve the annotation, allowing `_coerce_generic()` to reconstruct values into their designated dataclass/model types.\n\n---\n\n### Impact\n\nUsers annotating activities or orchestrators with `dict[str, MyModel]`, `Mapping[str, MyModel]`, or `tuple[MyModel, ...]` receive uncoerced dictionaries and lists, bypassing type validation and failing at runtime during attribute access.\n\n---\n\n### Suggested Direction\n\nUpdate `_can_reconstruct()` in `durabletask/serialization.py`:\n\n```python\n if origin in (dict, Mapping):\n return len(args) == 2 and converter.can_reconstruct(args[1])\n if origin is tuple:\n if not args:\n return False\n if len(args) == 2 and args[1] is Ellipsis:\n return converter.can_reconstruct(args[0])\n return any(converter.can_reconstruct(a) for a in args)\n```\n\n---\n\ncc @andystaples @berndverst\n","author":{"url":"https://github.com/hardikkaurani","@type":"Person","name":"hardikkaurani"},"datePublished":"2026-11-05T05:19:58Z","url":"https://github.com/microsoft/durabletask-python/issues/344"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f438b1e4-d394-d1c5-fb75-c5f8f826ebfc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D450:306EC8:C318F9:1128644:6AFE2CE3 |
| html-safe-nonce | 4909831cad5d264ec1f3a10bc1ee6ea3c4c09737fdf8a61bc8f8db2f92a1b8c8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENDUwOjMwNkVDODpDMzE4Rjk6MTEyODY0NDo2QUZFMkNFMyIsInZpc2l0b3JfaWQiOiI1NDIyNTUyNjg3MjQzNTcwNDAzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6164b4f6a874edde14a4c066d943a00ecafa68dd271bdc5f14f4b8be5dd65f48 |
| hovercard-subject-tag | issue:6217320187 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/durabletask-python/344/issue_layout |
| twitter:image | https://opengraph.githubassets.com/34fd7d07543694178214d57a4c5019c17d0fd16b3bb72d1c7ced3f03b91390c2/microsoft/durabletask-python/issues/344 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/34fd7d07543694178214d57a4c5019c17d0fd16b3bb72d1c7ced3f03b91390c2/microsoft/durabletask-python/issues/344 |
| og:image:alt | Problem Description In durabletask/serialization.py, _coerce_generic() supports typed coercion for dict / Mapping and tuple containers: https://github.com/microsoft/durabletask-python/blob/06bb2776... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hardikkaurani |
| hostname | github.com |
| expected-hostname | github.com |
| None | e973f9ec3625f3faad8cac5c2d11a214b7ffe45f3cc563b828459b290ecaeed3 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/durabletask-python git https://github.com/microsoft/durabletask-python.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 627664515 |
| octolytics-dimension-repository_nwo | microsoft/durabletask-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 627664515 |
| octolytics-dimension-repository_network_root_nwo | microsoft/durabletask-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 1f92c367595b9190e0be293896078033886b1913 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width