@@ -84,6 +84,12 @@ def collect_response_text(response: Any) -> str:
8484def decode_json_payload (text : str ) -> dict [str , Any ]:
8585 candidate = text .strip ()
8686
87+ # Helper to create a preview snippet for error messages
88+ def _snippet (s : str , max_len : int = 200 ) -> str :
89+ if len (s ) <= max_len :
90+ return s
91+ return s [:max_len ] + "..."
92+
8793 if candidate .startswith ("```" ):
8894 candidate = candidate .strip ("`" ).strip ()
8995 if candidate .startswith ("json" ):
@@ -94,20 +100,24 @@ def decode_json_payload(text: str) -> dict[str, Any]:
94100 end = candidate .rfind ("}" )
95101 if start == - 1 or end == - 1 :
96102 raise DeepResearchError (
97- "Unable to locate JSON object in response."
103+ f"Unable to locate JSON object in response. "
104+ f"Received: { _snippet (text )!r} "
98105 )
99106 candidate = candidate [start :end + 1 ]
100107
101108 try :
102109 payload = json .loads (candidate )
103110 except json .JSONDecodeError as exc :
104111 raise DeepResearchError (
105- "Deep research response was not valid JSON."
112+ f"Deep research response was not valid JSON. "
113+ f"Parse error at position { exc .pos } : { exc .msg } . "
114+ f"Content: { _snippet (candidate )!r} "
106115 ) from exc
107116
108117 if not isinstance (payload , dict ):
109118 raise DeepResearchError (
110- "Expected JSON object at top level of response."
119+ f"Expected JSON object at top level of response, "
120+ f"got { type (payload ).__name__ } : { _snippet (str (payload ))!r} "
111121 )
112122
113123 return payload
0 commit comments