Skip to content

Commit 97821c4

Browse files
Split Summarize PDF method by response file type
- Add additional method to summarize to output file - Use PdfRestFileBasedResponse for summarize to file Assisted-by: Codex
1 parent ab2e6d8 commit 97821c4

5 files changed

Lines changed: 256 additions & 28 deletions

File tree

src/pdfrest/client.py

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
translate_httpx_error,
6161
)
6262
from .models import (
63-
PdfRestDeletionResponse,
6463
ConvertToMarkdownResponse,
6564
ExtractTextResponse,
65+
PdfRestDeletionResponse,
6666
PdfRestErrorResponse,
6767
PdfRestFile,
6868
PdfRestFileBasedResponse,
@@ -78,14 +78,14 @@
7878
from .models._internal import (
7979
BasePdfRestGraphicPayload,
8080
BmpPdfRestPayload,
81-
DeletePayload,
8281
ConvertToMarkdownPayload,
82+
DeletePayload,
8383
ExtractImagesPayload,
8484
ExtractTextPayload,
8585
GifPdfRestPayload,
8686
JpegPdfRestPayload,
87-
PdfCompressPayload,
8887
OcrPdfPayload,
88+
PdfCompressPayload,
8989
PdfFlattenAnnotationsPayload,
9090
PdfFlattenFormsPayload,
9191
PdfFlattenTransparenciesPayload,
@@ -2134,21 +2134,24 @@ def summarize_pdf_text(
21342134
summary_format: SummaryFormat = "overview",
21352135
pages: PdfPageSelection | None = None,
21362136
output_format: SummaryOutputFormat = "markdown",
2137-
output_type: SummaryOutputType = "json",
21382137
output: str | None = None,
21392138
extra_query: Query | None = None,
21402139
extra_headers: AnyMapping | None = None,
21412140
extra_body: Body | None = None,
21422141
timeout: TimeoutTypes | None = None,
21432142
) -> SummarizePdfTextResponse:
2144-
"""Summarize the textual content of a PDF, Markdown, or text document."""
2143+
"""Summarize the textual content of a PDF, Markdown, or text document.
2144+
2145+
Always requests JSON output and returns the inline summary response defined in
2146+
the pdfRest API reference.
2147+
"""
21452148

21462149
payload: dict[str, Any] = {
21472150
"files": file,
21482151
"target_word_count": target_word_count,
21492152
"summary_format": summary_format,
21502153
"output_format": output_format,
2151-
"output_type": output_type,
2154+
"output_type": "json",
21522155
}
21532156
if pages is not None:
21542157
payload["pages"] = pages
@@ -2170,6 +2173,44 @@ def summarize_pdf_text(
21702173
raw_payload = self._send_request(request)
21712174
return SummarizePdfTextResponse.model_validate(raw_payload)
21722175

2176+
def summarize_pdf_text_to_file(
2177+
self,
2178+
file: PdfRestFile | Sequence[PdfRestFile],
2179+
*,
2180+
target_word_count: int | None = 400,
2181+
summary_format: SummaryFormat = "overview",
2182+
pages: PdfPageSelection | None = None,
2183+
output_format: SummaryOutputFormat = "markdown",
2184+
output: str | None = None,
2185+
extra_query: Query | None = None,
2186+
extra_headers: AnyMapping | None = None,
2187+
extra_body: Body | None = None,
2188+
timeout: TimeoutTypes | None = None,
2189+
) -> PdfRestFileBasedResponse:
2190+
"""Summarize a document and return the result as a downloadable file."""
2191+
2192+
payload: dict[str, Any] = {
2193+
"files": file,
2194+
"target_word_count": target_word_count,
2195+
"summary_format": summary_format,
2196+
"output_format": output_format,
2197+
"output_type": "file",
2198+
}
2199+
if pages is not None:
2200+
payload["pages"] = pages
2201+
if output is not None:
2202+
payload["output"] = output
2203+
2204+
return self._post_file_operation(
2205+
endpoint="/summarized-pdf-text",
2206+
payload=payload,
2207+
payload_model=SummarizePdfTextPayload,
2208+
extra_query=extra_query,
2209+
extra_headers=extra_headers,
2210+
extra_body=extra_body,
2211+
timeout=timeout,
2212+
)
2213+
21732214
def convert_to_markdown(
21742215
self,
21752216
file: PdfRestFile | Sequence[PdfRestFile],
@@ -2668,7 +2709,6 @@ def compress_pdf(
26682709
extra_body=extra_body,
26692710
timeout=timeout,
26702711
)
2671-
26722712

26732713
def flatten_transparencies(
26742714
self,
@@ -3083,21 +3123,24 @@ async def summarize_pdf_text(
30833123
summary_format: SummaryFormat = "overview",
30843124
pages: PdfPageSelection | None = None,
30853125
output_format: SummaryOutputFormat = "markdown",
3086-
output_type: SummaryOutputType = "json",
30873126
output: str | None = None,
30883127
extra_query: Query | None = None,
30893128
extra_headers: AnyMapping | None = None,
30903129
extra_body: Body | None = None,
30913130
timeout: TimeoutTypes | None = None,
30923131
) -> SummarizePdfTextResponse:
3093-
"""Summarize the textual content of a PDF, Markdown, or text document."""
3132+
"""Summarize the textual content of a PDF, Markdown, or text document.
3133+
3134+
Always requests JSON output and returns the inline summary response defined in
3135+
the pdfRest API reference.
3136+
"""
30943137

30953138
payload: dict[str, Any] = {
30963139
"files": file,
30973140
"target_word_count": target_word_count,
30983141
"summary_format": summary_format,
30993142
"output_format": output_format,
3100-
"output_type": output_type,
3143+
"output_type": "json",
31013144
}
31023145
if pages is not None:
31033146
payload["pages"] = pages
@@ -3119,6 +3162,44 @@ async def summarize_pdf_text(
31193162
raw_payload = await self._send_request(request)
31203163
return SummarizePdfTextResponse.model_validate(raw_payload)
31213164

3165+
async def summarize_pdf_text_to_file(
3166+
self,
3167+
file: PdfRestFile | Sequence[PdfRestFile],
3168+
*,
3169+
target_word_count: int | None = 400,
3170+
summary_format: SummaryFormat = "overview",
3171+
pages: PdfPageSelection | None = None,
3172+
output_format: SummaryOutputFormat = "markdown",
3173+
output: str | None = None,
3174+
extra_query: Query | None = None,
3175+
extra_headers: AnyMapping | None = None,
3176+
extra_body: Body | None = None,
3177+
timeout: TimeoutTypes | None = None,
3178+
) -> PdfRestFileBasedResponse:
3179+
"""Summarize a document and return the result as a downloadable file."""
3180+
3181+
payload: dict[str, Any] = {
3182+
"files": file,
3183+
"target_word_count": target_word_count,
3184+
"summary_format": summary_format,
3185+
"output_format": output_format,
3186+
"output_type": "file",
3187+
}
3188+
if pages is not None:
3189+
payload["pages"] = pages
3190+
if output is not None:
3191+
payload["output"] = output
3192+
3193+
return await self._post_file_operation(
3194+
endpoint="/summarized-pdf-text",
3195+
payload=payload,
3196+
payload_model=SummarizePdfTextPayload,
3197+
extra_query=extra_query,
3198+
extra_headers=extra_headers,
3199+
extra_body=extra_body,
3200+
timeout=timeout,
3201+
)
3202+
31223203
async def convert_to_markdown(
31233204
self,
31243205
file: PdfRestFile | Sequence[PdfRestFile],
@@ -3659,7 +3740,6 @@ async def compress_pdf(
36593740
extra_body=extra_body,
36603741
timeout=timeout,
36613742
)
3662-
36633743

36643744
async def flatten_transparencies(
36653745
self,
@@ -3687,7 +3767,7 @@ async def flatten_transparencies(
36873767
extra_body=extra_body,
36883768
timeout=timeout,
36893769
)
3690-
3770+
36913771
async def linearize_pdf(
36923772
self,
36933773
file: PdfRestFile | Sequence[PdfRestFile],

src/pdfrest/models/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .public import (
2-
PdfRestDeletionResponse,
32
ConvertToMarkdownResponse,
43
ExtractTextResponse,
4+
PdfRestDeletionResponse,
55
PdfRestErrorResponse,
66
PdfRestFile,
77
PdfRestFileBasedResponse,
@@ -13,9 +13,9 @@
1313
)
1414

1515
__all__ = [
16-
"PdfRestDeletionResponse",
1716
"ConvertToMarkdownResponse",
1817
"ExtractTextResponse",
18+
"PdfRestDeletionResponse",
1919
"PdfRestErrorResponse",
2020
"PdfRestFile",
2121
"PdfRestFileBasedResponse",

src/pdfrest/models/public.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
from typing_extensions import override
2121

2222
__all__ = (
23-
"PdfRestDeletionResponse",
2423
"ConvertToMarkdownResponse",
2524
"ExtractTextResponse",
25+
"PdfRestDeletionResponse",
2626
"PdfRestErrorResponse",
2727
"PdfRestFile",
2828
"PdfRestFileBasedResponse",
@@ -314,6 +314,8 @@ class PdfRestDeletionResponse(BaseModel):
314314
min_length=1,
315315
),
316316
]
317+
318+
317319
class SummarizePdfTextResponse(BaseModel):
318320
"""Response returned by the summarize-pdf-text tool."""
319321

tests/live/test_live_summarize_pdf_text.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from pdfrest import PdfRestApiError, PdfRestClient
6-
from pdfrest.models import SummarizePdfTextResponse
6+
from pdfrest.models import PdfRestFileBasedResponse, SummarizePdfTextResponse
77

88
from ..resources import get_test_resource_path
99

@@ -21,7 +21,6 @@ def test_live_summarize_pdf_text_success(
2121
response = client.summarize_pdf_text(
2222
uploaded,
2323
target_word_count=40,
24-
output_type="json",
2524
summary_format="overview",
2625
)
2726

@@ -30,6 +29,28 @@ def test_live_summarize_pdf_text_success(
3029
assert response.input_id == uploaded.id
3130

3231

32+
def test_live_summarize_pdf_text_to_file_success(
33+
pdfrest_api_key: str,
34+
pdfrest_live_base_url: str,
35+
) -> None:
36+
resource = get_test_resource_path("report.pdf")
37+
with PdfRestClient(
38+
api_key=pdfrest_api_key,
39+
base_url=pdfrest_live_base_url,
40+
) as client:
41+
uploaded = client.files.create_from_paths([resource])[0]
42+
response = client.summarize_pdf_text_to_file(
43+
uploaded,
44+
target_word_count=40,
45+
summary_format="overview",
46+
)
47+
48+
assert isinstance(response, PdfRestFileBasedResponse)
49+
assert response.output_files
50+
assert response.output_file.id
51+
assert response.input_id == uploaded.id
52+
53+
3354
def test_live_summarize_pdf_text_invalid_format(
3455
pdfrest_api_key: str,
3556
pdfrest_live_base_url: str,

0 commit comments

Comments
 (0)