xberg 1.0.4

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 98 formats and 306 programming languages via tree-sitter code intelligence with async/sync APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Per-chunk multi-label LLM classification driver.
//!
//! Iterates `ExtractedDocument::chunks`, groups them into bounded-size batches to
//! amortize the fixed cost of a (potentially large) label-definition prompt block,
//! and runs those batches with bounded concurrency against the configured LLM
//! using [`crate::llm::structured::complete_with_json_schema`].
//!
//! Unlike [`super::page_classifier`], every chunk always receives zero, one, or
//! many labels — there is no single-label mode.

use std::collections::HashMap;
use std::sync::Arc;

use serde_json::{Value, json};

use crate::core::config::{ChunkClassificationConfig, ChunkClassificationDefinition};
use crate::types::classification::ClassificationLabel;
use crate::types::{ExtractedDocument, LlmUsage};

/// Default Jinja2 template used when `ChunkClassificationConfig::prompt_template`
/// is `None`. Variables: `definitions` (rendered label + description list),
/// `chunks` (numbered chunk texts in the current batch).
pub const DEFAULT_CHUNK_CLASSIFICATION_TEMPLATE: &str = "\
You are a precise document classification system operating on chunks of a larger \
document. Each chunk may match zero, one, or multiple of the following label \
definitions:

{{ definitions }}

For every numbered chunk below, return every label that applies. Order is not \
significant. If no label fits a chunk, return an empty list for it. Do not invent \
labels not in the list above.

Chunks:
{{ chunks }}

Respond as JSON that matches the provided schema, with one entry per chunk index.";

/// Render the definitions block: one `- label: description` line per entry.
fn render_definitions(definitions: &[ChunkClassificationDefinition]) -> String {
    definitions
        .iter()
        .map(|d| format!("- {}: {}", d.label, d.description))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Render the numbered chunk-text block for a single batch.
fn render_chunks_block(batch: &[(usize, String)]) -> String {
    batch
        .iter()
        .map(|(index, text)| format!("[{index}]\n{text}"))
        .collect::<Vec<_>>()
        .join("\n\n")
}

/// Build the JSON schema constraining the LLM's batch response.
///
/// Shape: `{"results": [{"index": int, "labels": [{"label": str, "confidence": float?}]}]}`.
fn build_schema(definitions: &[ChunkClassificationDefinition]) -> Value {
    let label_enum: Vec<Value> = definitions.iter().map(|d| Value::String(d.label.clone())).collect();
    let label_object = json!({
        "type": "object",
        "properties": {
            "label": { "type": "string", "enum": label_enum },
            "confidence": { "type": "number", "minimum": 0.0, "maximum": 1.0 },
        },
        "required": ["label"],
        "additionalProperties": false,
    });

    json!({
        "type": "object",
        "properties": {
            "results": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "index": { "type": "integer" },
                        "labels": { "type": "array", "items": label_object },
                    },
                    "required": ["index", "labels"],
                    "additionalProperties": false,
                },
            },
        },
        "required": ["results"],
        "additionalProperties": false,
    })
}

/// Parse the LLM's batch JSON response into a `chunk_index -> labels` map.
///
/// Entries whose `index` does not correspond to a chunk in the batch are kept as
/// keyed by whatever index the model reported; the caller only applies entries
/// that match a real chunk index, so a hallucinated index is silently dropped.
fn parse_batch_response(value: &Value) -> HashMap<usize, Vec<ClassificationLabel>> {
    let mut out = HashMap::new();
    let Some(results) = value.get("results").and_then(|v| v.as_array()) else {
        return out;
    };
    for entry in results {
        let Some(index) = entry.get("index").and_then(|v| v.as_u64()) else {
            continue;
        };
        let mut labels = Vec::new();
        if let Some(arr) = entry.get("labels").and_then(|v| v.as_array()) {
            for label_entry in arr {
                if let Some(label) = label_entry.get("label").and_then(|v| v.as_str()) {
                    labels.push(ClassificationLabel {
                        label: label.to_string(),
                        confidence: label_entry.get("confidence").and_then(|v| v.as_f64()).map(|f| f as f32),
                    });
                }
            }
        }
        out.insert(index as usize, labels);
    }
    out
}

/// Pre-rendered classification context shared across every batch.
struct ClassifyChunkContext {
    template: String,
    definitions_rendered: String,
    schema: Value,
}

impl ClassifyChunkContext {
    fn new(config: &ChunkClassificationConfig) -> Self {
        let template = config
            .prompt_template
            .clone()
            .unwrap_or_else(|| DEFAULT_CHUNK_CLASSIFICATION_TEMPLATE.to_string());
        Self {
            template,
            definitions_rendered: render_definitions(&config.definitions),
            schema: build_schema(&config.definitions),
        }
    }
}

/// Classify a single batch of `(chunk_index, chunk_text)` pairs.
///
/// Returns the parsed `chunk_index -> labels` map (only for chunks the model
/// actually reported) plus the LLM call's usage record, if any.
async fn classify_batch(
    batch: &[(usize, String)],
    ctx: &ClassifyChunkContext,
    llm_config: &crate::core::config::LlmConfig,
) -> crate::Result<(HashMap<usize, Vec<ClassificationLabel>>, Option<LlmUsage>)> {
    let chunks_rendered = render_chunks_block(batch);
    let render_ctx = minijinja::context! {
        definitions => &ctx.definitions_rendered,
        chunks => &chunks_rendered,
    };
    let prompt = crate::llm::prompts::render_template(&ctx.template, &render_ctx)?;

    let (value, usage) = crate::llm::structured::complete_with_json_schema(
        llm_config,
        &prompt,
        "chunk_classification_multi",
        &ctx.schema,
        "chunk_classification",
    )
    .await?;

    Ok((parse_batch_response(&value), usage))
}

/// Run chunk classification against an extraction result.
///
/// Mutates `ChunkMetadata::classifications` on every chunk in
/// `result.chunks` and appends every LLM call's usage to `result.llm_usage`.
/// A chunk whose classification batch call fails (or that the model omitted
/// from its response) is simply left with an empty `classifications` vector for
/// that chunk, unless the failure is a validation error (empty config) or every
/// batch task fails, in which case the first error is returned.
///
/// # Errors
///
/// Returns [`crate::XbergError::Validation`] when `config.definitions` is empty.
/// Returns the first batch error encountered when rendering the prompt or
/// calling the LLM fails for every batch; partial failures on a subset of
/// batches are recorded as `ProcessingWarning`s by the caller instead of
/// aborting the whole run (see
/// [`crate::plugins::processor::builtin::chunk_classification`]).
pub async fn classify_chunks(result: &mut ExtractedDocument, config: &ChunkClassificationConfig) -> crate::Result<()> {
    if config.definitions.is_empty() {
        return Err(crate::XbergError::validation(
            "ChunkClassificationConfig.definitions must contain at least one entry",
        ));
    }

    let Some(chunks) = result.chunks.as_ref() else {
        return Ok(());
    };
    if chunks.is_empty() {
        return Ok(());
    }

    let batch_size = config.batch_size.max(1);
    let max_concurrency = config.max_concurrency.max(1);

    let batches: Vec<Vec<(usize, String)>> = chunks
        .iter()
        .enumerate()
        .filter(|(_, chunk)| !chunk.content.is_empty())
        .map(|(index, chunk)| (index, chunk.content.clone()))
        .collect::<Vec<_>>()
        .chunks(batch_size)
        .map(<[(usize, String)]>::to_vec)
        .collect();

    if batches.is_empty() {
        return Ok(());
    }

    let ctx = Arc::new(ClassifyChunkContext::new(config));
    let llm_config = Arc::new(config.llm.clone());
    let semaphore = Arc::new(tokio::sync::Semaphore::new(max_concurrency));

    let mut join_set = tokio::task::JoinSet::new();
    for batch in batches {
        let ctx = Arc::clone(&ctx);
        let llm_config = Arc::clone(&llm_config);
        let semaphore = Arc::clone(&semaphore);
        join_set.spawn(async move {
            let _permit = semaphore.acquire_owned().await.map_err(|_| {
                crate::XbergError::Other("chunk-classification concurrency semaphore closed unexpectedly".to_string())
            })?;
            classify_batch(&batch, &ctx, &llm_config).await
        });
    }

    let mut per_chunk: HashMap<usize, Vec<ClassificationLabel>> = HashMap::new();
    let mut usages: Vec<LlmUsage> = Vec::new();
    let mut first_error: Option<crate::XbergError> = None;
    let mut any_success = false;

    while let Some(joined) = join_set.join_next().await {
        let batch_result = match joined {
            Ok(inner) => inner,
            Err(join_err) => Err(crate::XbergError::Other(format!(
                "chunk classification batch task failed to complete: {join_err}"
            ))),
        };
        match batch_result {
            Ok((labels_by_index, usage)) => {
                any_success = true;
                per_chunk.extend(labels_by_index);
                if let Some(u) = usage {
                    usages.push(u);
                }
            }
            Err(err) => {
                if first_error.is_none() {
                    first_error = Some(err);
                }
            }
        }
    }

    if !any_success && let Some(err) = first_error {
        return Err(err);
    }

    if let Some(chunks_mut) = result.chunks.as_mut() {
        apply_batch_results(chunks_mut, per_chunk);
    }

    if !usages.is_empty() {
        result.llm_usage.get_or_insert_with(Vec::new).extend(usages);
    }

    Ok(())
}

/// Write each batch's parsed labels onto the matching chunk's
/// `ChunkMetadata::classifications`. Chunks with no entry in `per_chunk`
/// (skipped as empty, or dropped by a failed batch) are left untouched, so
/// their `classifications` stays whatever it already was (empty, by
/// construction, before this stage runs).
fn apply_batch_results(chunks: &mut [crate::types::Chunk], mut per_chunk: HashMap<usize, Vec<ClassificationLabel>>) {
    for (index, chunk) in chunks.iter_mut().enumerate() {
        if let Some(labels) = per_chunk.remove(&index) {
            chunk.metadata.classifications = labels;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn definitions() -> Vec<ChunkClassificationDefinition> {
        vec![
            ChunkClassificationDefinition {
                label: "director_appointment".to_string(),
                description: "A person is appointed, elected, or designated as a director.".to_string(),
            },
            ChunkClassificationDefinition {
                label: "director_resignation".to_string(),
                description: "A director resigned, retired, or was removed.".to_string(),
            },
            ChunkClassificationDefinition {
                label: "registered_office_change".to_string(),
                description: "The registered office or legal address of an entity changes.".to_string(),
            },
        ]
    }

    #[test]
    fn build_schema_uses_labels_array_with_enum() {
        let schema = build_schema(&definitions());
        assert_eq!(schema["properties"]["results"]["type"], "array");
        let label_enum =
            &schema["properties"]["results"]["items"]["properties"]["labels"]["items"]["properties"]["label"]["enum"];
        assert_eq!(label_enum.as_array().unwrap().len(), 3);
    }

    #[test]
    fn render_definitions_joins_label_and_description() {
        let rendered = render_definitions(&definitions());
        assert_eq!(
            rendered,
            "- director_appointment: A person is appointed, elected, or designated as a director.\n\
             - director_resignation: A director resigned, retired, or was removed.\n\
             - registered_office_change: The registered office or legal address of an entity changes."
        );
    }

    #[test]
    fn render_chunks_block_numbers_each_chunk() {
        let batch = vec![(0usize, "alpha text".to_string()), (3usize, "beta text".to_string())];
        let rendered = render_chunks_block(&batch);
        assert_eq!(rendered, "[0]\nalpha text\n\n[3]\nbeta text");
    }

    #[test]
    fn parse_batch_response_maps_multiple_chunks_to_their_labels() {
        let payload = json!({
            "results": [
                {
                    "index": 0,
                    "labels": [
                        {"label": "director_appointment", "confidence": 0.93},
                        {"label": "registered_office_change", "confidence": 0.81},
                    ]
                },
                {
                    "index": 1,
                    "labels": []
                },
            ]
        });
        let parsed = parse_batch_response(&payload);
        assert_eq!(parsed.len(), 2);
        let chunk0 = &parsed[&0];
        assert_eq!(chunk0.len(), 2);
        assert_eq!(chunk0[0].label, "director_appointment");
        assert_eq!(chunk0[0].confidence, Some(0.93));
        assert_eq!(chunk0[1].label, "registered_office_change");
        assert_eq!(chunk0[1].confidence, Some(0.81));
        assert!(parsed[&1].is_empty());
    }

    #[test]
    fn parse_batch_response_drops_entries_missing_an_index() {
        let payload = json!({
            "results": [
                {"labels": [{"label": "director_appointment"}]},
                {"index": 2, "labels": [{"label": "director_resignation"}]},
            ]
        });
        let parsed = parse_batch_response(&payload);
        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[&2][0].label, "director_resignation");
    }

    #[test]
    fn parse_batch_response_empty_results_yields_empty_map() {
        let parsed = parse_batch_response(&json!({"results": []}));
        assert!(parsed.is_empty());
    }

    fn empty_chunk(index: usize) -> crate::types::Chunk {
        crate::types::Chunk {
            content: format!("chunk {index}"),
            chunk_type: Default::default(),
            embedding: None,
            metadata: crate::types::ChunkMetadata {
                byte_start: 0,
                byte_end: 0,
                token_count: None,
                chunk_index: index,
                total_chunks: 3,
                first_page: None,
                last_page: None,
                heading_context: None,
                heading_path: Vec::new(),
                image_indices: Vec::new(),
                node_ids: Vec::new(),
                page_spans: Vec::new(),
                classifications: Vec::new(),
            },
        }
    }

    #[test]
    fn apply_batch_results_writes_multi_label_results_onto_matching_chunks() {
        let mut chunks = vec![empty_chunk(0), empty_chunk(1), empty_chunk(2)];
        let mut per_chunk = HashMap::new();
        per_chunk.insert(
            0,
            vec![
                ClassificationLabel {
                    label: "director_appointment".to_string(),
                    confidence: Some(0.93),
                },
                ClassificationLabel {
                    label: "registered_office_change".to_string(),
                    confidence: Some(0.81),
                },
            ],
        );
        per_chunk.insert(2, vec![]);

        apply_batch_results(&mut chunks, per_chunk);

        assert_eq!(chunks[0].metadata.classifications.len(), 2);
        assert_eq!(chunks[0].metadata.classifications[0].label, "director_appointment");
        assert_eq!(chunks[0].metadata.classifications[1].label, "registered_office_change");
        assert!(chunks[1].metadata.classifications.is_empty());
        assert!(chunks[2].metadata.classifications.is_empty());
    }

    #[tokio::test]
    async fn classify_chunks_returns_validation_error_when_definitions_empty() {
        let config = ChunkClassificationConfig {
            prompt_template: None,
            definitions: Vec::new(),
            llm: crate::core::config::LlmConfig {
                model: "openai/gpt-4o-mini".to_string(),
                ..Default::default()
            },
            batch_size: 10,
            max_concurrency: 4,
        };
        let mut result = ExtractedDocument {
            content: "x".to_string(),
            mime_type: std::borrow::Cow::Borrowed("text/plain"),
            ..Default::default()
        };
        let err = classify_chunks(&mut result, &config).await.unwrap_err();
        assert!(err.to_string().contains("definitions must contain at least one entry"));
    }

    #[tokio::test]
    async fn classify_chunks_is_noop_when_no_chunks_present() {
        let config = ChunkClassificationConfig {
            prompt_template: None,
            definitions: definitions(),
            llm: crate::core::config::LlmConfig {
                model: "openai/gpt-4o-mini".to_string(),
                ..Default::default()
            },
            batch_size: 10,
            max_concurrency: 4,
        };
        let mut result = ExtractedDocument {
            content: "x".to_string(),
            mime_type: std::borrow::Cow::Borrowed("text/plain"),
            chunks: None,
            ..Default::default()
        };
        classify_chunks(&mut result, &config).await.unwrap();
        assert!(result.chunks.is_none());
    }
}