diff --git a/src/parse_bench/inference/providers/parse/nutrient_dws.py b/src/parse_bench/inference/providers/parse/nutrient_dws.py index 7d960768..c09af665 100644 --- a/src/parse_bench/inference/providers/parse/nutrient_dws.py +++ b/src/parse_bench/inference/providers/parse/nutrient_dws.py @@ -1,8 +1,9 @@ """Provider for Nutrient DWS Data Extraction PARSE. -Only the options in the published contract are sent (``format``/``formats``, -``includeWords``). Unknown options are accepted and ignored rather than rejected, -and some combinations degrade the markdown silently. +Only options from the published contract are sent (``format``/``formats``, +``includeWords``, ``enableInlineFormatting``, and with the hosted body +``includeHeadersAndFooters`` and ``extractWordsFromPictures``). Unknown options are accepted and +ignored rather than rejected. Credits are charged per page. ``usage.data_extraction_credits.cost`` is recorded on ``raw_output`` as ``credits_used``; its sibling ``remainingCredits`` is not a @@ -167,16 +168,26 @@ def credit_rate_usd(self) -> float | None: def _instructions(self) -> str: # `text` mode takes the singular `format`; the spatial modes take # `formats` and additionally return `output.elements`. - # Contract options only. `useHtmlTables` / - # `enableSemanticBlockFormatting` / `includeHeadersAndFooters` are not in - # the contract and that combination strips chart tables and interleaves - # running headers into the body. `includeWords` defaults to false and - # carries the style flags the formatting rules score. + # `includeWords` defaults to false and carries the style flags the + # formatting rules score. `enableInlineFormatting` defaults to false and + # writes those styles into `output.markdown` (the body when `body_source` + # is "hosted"); the graph body styles itself from the word flags. + # When `output.markdown` is the graded body, `includeHeadersAndFooters` + # and `extractWordsFromPictures` (both default false) are switched on: + # the graph body keeps running headers, footers and the words inside + # pictures, so the hosted body is asked for the same content. The graph + # body never reads `output.markdown`, so its request is unchanged. The + # other Markdown switches (`useHtmlTables`, `enableSemanticBlockFormatting`) + # keep the defaults DWS documents and pins, so they aren't sent. if self._spatial: output: dict[str, Any] = { "formats": ["markdown", "spatial"], "includeWords": True, + "enableInlineFormatting": True, } + if self._body_source == "hosted": + output["includeHeadersAndFooters"] = True + output["extractWordsFromPictures"] = True else: output = {"format": "markdown"} return json.dumps({"mode": self._mode, "output": output}) diff --git a/tests/parse_bench/inference/providers/parse/test_nutrient_dws_errors.py b/tests/parse_bench/inference/providers/parse/test_nutrient_dws_errors.py index 2f47a758..4c707075 100644 --- a/tests/parse_bench/inference/providers/parse/test_nutrient_dws_errors.py +++ b/tests/parse_bench/inference/providers/parse/test_nutrient_dws_errors.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json import os import unittest from pathlib import Path @@ -98,5 +99,52 @@ def test_rate_is_overridable(self) -> None: self.assertEqual(self._provider(credit_rate_usd=0.0012).credit_rate_usd, 0.0012) +class TestInstructions(unittest.TestCase): + """The options each mode sends.""" + + def test_spatial_modes_ask_for_words_and_inline_formatting(self) -> None: + for mode in ("structure", "understand", "agentic"): + provider = NutrientDwsProvider("nutrient_dws", {"mode": mode, "api_key": "test"}) + self.assertEqual( + json.loads(provider._instructions()), + { + "mode": mode, + "output": { + "formats": ["markdown", "spatial"], + "includeWords": True, + "enableInlineFormatting": True, + }, + }, + ) + + def test_hosted_body_also_asks_for_headers_footers_and_picture_words(self) -> None: + for mode in ("structure", "understand", "agentic"): + provider = NutrientDwsProvider("nutrient_dws", {"mode": mode, "body_source": "hosted", "api_key": "test"}) + self.assertEqual( + json.loads(provider._instructions()), + { + "mode": mode, + "output": { + "formats": ["markdown", "spatial"], + "includeWords": True, + "enableInlineFormatting": True, + "includeHeadersAndFooters": True, + "extractWordsFromPictures": True, + }, + }, + ) + + def test_hosted_body_from_environment_also_asks_for_them(self) -> None: + with mock.patch.dict(os.environ, {"NUTRIENT_DWS_BODY_SOURCE": "hosted"}): + provider = NutrientDwsProvider("nutrient_dws", {"mode": "understand", "api_key": "test"}) + output = json.loads(provider._instructions())["output"] + self.assertIs(output["includeHeadersAndFooters"], True) + self.assertIs(output["extractWordsFromPictures"], True) + + def test_text_mode_sends_only_the_markdown_format(self) -> None: + provider = NutrientDwsProvider("nutrient_dws", {"mode": "text", "api_key": "test"}) + self.assertEqual(json.loads(provider._instructions()), {"mode": "text", "output": {"format": "markdown"}}) + + if __name__ == "__main__": unittest.main()