@@ -118,17 +118,32 @@ def requires_chunks(cls) -> bool:
118118 """
119119 return False
120120
121- def begin_chunks (self , filename : str , temp_dir : Path ) -> None :
121+ def begin_chunks (
122+ self ,
123+ filename : str ,
124+ temp_dir : Path ,
125+ chunk_target_key : Optional [str ] = None ,
126+ ) -> None :
122127 """Called once before the first chunk of a document is streamed.
123128
124129 Default: open a temp ``{stem}.chunks.jsonl`` file for writing.
125130 DB processors that override ``requires_chunks()`` can use this to
126131 initialise per-document state instead.
132+
133+ ``chunk_target_key``, when provided, is the storage path that
134+ ``end_chunks`` will pass to ``upload_file()`` as *target_filename*.
135+ When omitted the bare filename (``{stem}.chunks.jsonl``) is used,
136+ which is only correct for database processors that override
137+ ``end_chunks`` and never call ``upload_file()`` themselves.
127138 """
128- self ._chunk_jsonl_path : Optional [Path ] = (
129- temp_dir / f"{ Path (filename ).stem } .chunks.jsonl"
130- )
139+ stem = Path (filename ).stem
140+ self ._chunk_jsonl_path : Optional [Path ] = temp_dir / f"{ stem } .chunks.jsonl"
131141 self ._chunk_jsonl_file = self ._chunk_jsonl_path .open ("w" , encoding = "utf-8" )
142+ self ._chunk_target_key : Optional [str ] = (
143+ chunk_target_key
144+ if chunk_target_key is not None
145+ else self ._chunk_jsonl_path .name
146+ )
132147
133148 def consume_chunk (self , chunk : "ChunkedDocumentResultItem" ) -> None :
134149 """Called once per chunk as it is produced by the chunker.
@@ -154,6 +169,6 @@ def end_chunks(self) -> None:
154169 self ._chunk_jsonl_file .close ()
155170 self .upload_file (
156171 filename = self ._chunk_jsonl_path , # type: ignore[arg-type]
157- target_filename = self ._chunk_jsonl_path . name , # type: ignore[union-attr ]
172+ target_filename = self ._chunk_target_key , # type: ignore[arg-type ]
158173 content_type = "application/jsonl" ,
159174 )
0 commit comments