fix(sdk): preserve JSON document downloads (#19367)

Fix SDK Document.download() — check Content-Disposition: attachment before JSON parsing. Arrays/scalars/plain files now work.
This commit is contained in:
Wenchao An authored and GitHub committed 2026-09-09 10:05:04 +08:00
1 parent 9538c2825c
commit d5ebaf6f07
2 files changed
+97 -8

No files matched your search

+13 -8
View File
@@ -14,11 +14,13 @@
# limitations under the License.
#
import json
import logging
from .base import Base
from .chunk import Chunk
logger = logging.getLogger(__name__)
class Document(Base):
class ParserConfig(Base):
@@ -64,16 +66,19 @@ class Document(Base):
def download(self):
res = self.get(f"/datasets/{self.dataset_id}/documents/{self.id}")
error_keys = set(["code", "message"])
content_disposition = res.headers.get("Content-Disposition", "")
if content_disposition.lstrip().lower().startswith("attachment"):
logger.debug("Document.download returning attachment content dataset_id=%s document_id=%s", self.dataset_id, self.id)
return res.content
try:
response = res.json()
actual_keys = set(response.keys())
if actual_keys == error_keys:
if isinstance(response, dict) and "code" in response and response["code"] != 0:
raise Exception(response.get("message"))
else:
return res.content
except json.JSONDecodeError:
return res.content
except ValueError:
pass
logger.debug("Document.download returning non-attachment content dataset_id=%s document_id=%s", self.dataset_id, self.id)
return res.content
def list_chunks(self, page=1, page_size=30, keywords="", id=""):
data = {"keywords": keywords, "page": page, "page_size": page_size, "id": id}