Skip to content

Commit e635cfd

Browse files
committed
feat(docx-parser, pdf-parser): Implement max_output_tokens for response generation
- Add max_output_tokens attribute to DocxFileParser and PDFFileParser for controlling token generation in responses - Update relevant methods to utilize max_output_tokens in generation configuration
1 parent a45c87a commit e635cfd

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

agentle/parsing/parsers/docx.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from rsb.models.field import Field
2121

2222

23+
from agentle.generations.models.generation.generation_config import GenerationConfig
2324
from agentle.generations.models.message_parts.file import FilePart
2425
from agentle.generations.models.structured_outputs_store.visual_media_description import (
2526
VisualMediaDescription,
@@ -182,6 +183,9 @@ class DocxFileParser(DocumentParser):
182183
Note: When this is enabled, most other configuration options are ignored as the AI handles all processing.
183184
"""
184185

186+
max_output_tokens: int | None = Field(default=None)
187+
"""Maximum number of tokens to generate in the response."""
188+
185189
async def parse_async(
186190
self,
187191
document_path: str,
@@ -517,6 +521,9 @@ def _try_convert_docx_to_pdf_headless(
517521
"Output clear, concise descriptions suitable for a 'Visual Content' section."
518522
),
519523
response_schema=VisualMediaDescription,
524+
generation_config=GenerationConfig(
525+
max_output_tokens=self.max_output_tokens
526+
),
520527
)
521528
page_description = agent_response.parsed.md
522529
image_cache[page_hash] = (page_description, "")
@@ -663,6 +670,7 @@ def _convert_docx_to_pdf(input_path: str, out_dir: str) -> str | None:
663670
model=self.model,
664671
use_native_pdf_processing=True,
665672
strategy=self.strategy,
673+
max_output_tokens=self.max_output_tokens,
666674
)
667675

668676
logger.debug("Delegating to PDFFileParser with native processing")

agentle/parsing/parsers/pdf.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ class PDFFileParser(DocumentParser):
258258
# Metrics state
259259
last_parse_metrics: PDFParseMetrics | None = None
260260

261+
max_output_tokens: int | None = Field(default=None)
262+
"""Maximum number of tokens to generate in the response."""
263+
261264
async def parse_async(self, document_path: str) -> ParsedFile:
262265
"""
263266
Asynchronously parse a PDF document and convert it to a structured representation.
@@ -741,7 +744,7 @@ async def _call_provider_with_retry(
741744
response_schema=VisualMediaDescription,
742745
generation_config=GenerationConfig(
743746
max_output_tokens=self.max_output_tokens
744-
)
747+
),
745748
),
746749
timeout=self.image_description_timeout,
747750
)
@@ -858,8 +861,7 @@ async def _parse_with_native_pdf_processing(self, document_path: str) -> ParsedF
858861
prompt=[pdf_file_part, prompt],
859862
response_schema=PDFPageExtraction,
860863
generation_config=GenerationConfig(
861-
timeout_s=300.0,
862-
max_output_tokens=self.max_output_tokens
864+
timeout_s=300.0, max_output_tokens=self.max_output_tokens
863865
),
864866
model=self.model,
865867
)

0 commit comments

Comments
 (0)