11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT License.
33
4+ import json
45from datetime import datetime
56from typing import Any , Optional
67
@@ -94,7 +95,13 @@ def history(self) -> Optional[list[Any]]:
9495 return None
9596
9697 def to_json (self ) -> dict [str , Any ]:
97- """Convert this status into a v1-compatible JSON dictionary."""
98+ """Convert this status into a v1-compatible JSON dictionary.
99+
100+ Payload fields (``output``, ``input``, ``customStatus``) are emitted as
101+ their raw JSON representation rather than the reconstructed Python
102+ objects, so the result is always JSON-serializable even when the
103+ orchestration payloads are custom types.
104+ """
98105 result : dict [str , Any ] = {}
99106 if self .name is not None :
100107 result ["name" ] = self .name
@@ -104,12 +111,32 @@ def to_json(self) -> dict[str, Any]:
104111 result ["createdTime" ] = self .created_time .isoformat ()
105112 if self .last_updated_time is not None :
106113 result ["lastUpdatedTime" ] = self .last_updated_time .isoformat ()
107- if self .output is not None :
108- result ["output" ] = self .output
109- if self .input_ is not None :
110- result ["input" ] = self .input_
114+ output = self ._raw_payload (
115+ self ._state .serialized_output if self ._state is not None else None )
116+ if output is not None :
117+ result ["output" ] = output
118+ input_ = self ._raw_payload (
119+ self ._state .serialized_input if self ._state is not None else None )
120+ if input_ is not None :
121+ result ["input" ] = input_
111122 if self .runtime_status is not None :
112123 result ["runtimeStatus" ] = self .runtime_status .name
113- if self .custom_status is not None :
114- result ["customStatus" ] = self .custom_status
124+ custom_status = self ._raw_payload (
125+ self ._state .serialized_custom_status if self ._state is not None else None )
126+ if custom_status is not None :
127+ result ["customStatus" ] = custom_status
115128 return result
129+
130+ @staticmethod
131+ def _raw_payload (serialized : Optional [str ]) -> Any :
132+ """Parse a serialized payload as plain JSON without reconstructing types.
133+
134+ Returns the parsed JSON value (which is always JSON-serializable), or the
135+ original string if it is not valid JSON, or ``None`` when absent.
136+ """
137+ if serialized is None :
138+ return None
139+ try :
140+ return json .loads (serialized )
141+ except (TypeError , ValueError ):
142+ return serialized
0 commit comments