Skip to content

Commit 30aaf02

Browse files
KlaijanConiferish
authored andcommitted
add text/csv handling to the python sdk
1 parent ba76667 commit 30aaf02

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/unstructured_client/_hooks/custom/request_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,12 @@ def create_response(elements: list) -> httpx.Response:
218218
Returns:
219219
The modified response object with updated content.
220220
"""
221-
response = httpx.Response(status_code=200, headers={"Content-Type": "application/json"})
222-
content = json.dumps(elements).encode()
221+
if not isinstance(elements[0], dict):
222+
response = httpx.Response(status_code=200, headers={"Content-Type": "text/csv"})
223+
content = b''.join(elements)
224+
else:
225+
response = httpx.Response(status_code=200, headers={"Content-Type": "application/json"})
226+
content = json.dumps(elements).encode()
223227
content_length = str(len(content))
224228
response.headers.update({"Content-Length": content_length})
225229
setattr(response, "_content", content)

src/unstructured_client/_hooks/custom/split_pdf_hook.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,13 @@ def _await_elements(
576576
response_number,
577577
)
578578
successful_responses.append(res)
579-
if self.cache_tmp_data_feature:
580-
elements.append(load_elements_from_response(res))
581-
else:
582-
elements.append(res.json())
579+
if res.headers["Content-Type"] == "application/json":
580+
if self.cache_tmp_data_feature:
581+
elements.append(load_elements_from_response(res))
582+
else:
583+
elements.append(res.json())
584+
else: # -- Response contains csv data
585+
elements.append(res.content)
583586
else:
584587
error_message = f"Failed to partition set {response_number}."
585588

@@ -591,7 +594,12 @@ def _await_elements(
591594

592595
self.api_successful_responses[operation_id] = successful_responses
593596
self.api_failed_responses[operation_id] = failed_responses
594-
flattened_elements = [element for sublist in elements for element in sublist]
597+
flattened_elements = []
598+
for sublist in elements:
599+
if isinstance(sublist, list):
600+
flattened_elements.extend(sublist)
601+
else:
602+
flattened_elements.append(sublist)
595603
return flattened_elements
596604

597605
def after_success(
@@ -613,7 +621,7 @@ def after_success(
613621
"""
614622
# Grab the correct id out of the dummy request
615623
operation_id = response.request.headers.get("operation_id")
616-
624+
# breakpoint();
617625
elements = self._await_elements(operation_id)
618626

619627
# if fails are disallowed, return the first failed response

0 commit comments

Comments
 (0)