Extract a ``Paper`` from a typed ``PaperInput``. See design doc §4.1 for the rationale on each kwarg. ``memory`` is a PR6 placeholder — non-None values are accepted but unused in PR5. Raises: UnsupportedContentTypeError: When fetched bytes are not PDF / HTML / markd
(
input: PaperInput,
*,
cfg: PaperFlowCfg | None = None,
extra_tools: list[Tool] | None = None,
extra_instructions: str | None = None,
output_type: type[P] | None = None,
memory: object | None = None,
extra_run_hooks: list[RunHooks[Any]] | None = None,
extra_input_guardrails: list[Any] | None = None,
extra_output_guardrails: list[Any] | None = None,
)
| 62 | |
| 63 | |
| 64 | async def paper_flow( |
| 65 | input: PaperInput, |
| 66 | *, |
| 67 | cfg: PaperFlowCfg | None = None, |
| 68 | extra_tools: list[Tool] | None = None, |
| 69 | extra_instructions: str | None = None, |
| 70 | output_type: type[P] | None = None, |
| 71 | memory: object | None = None, |
| 72 | extra_run_hooks: list[RunHooks[Any]] | None = None, |
| 73 | extra_input_guardrails: list[Any] | None = None, |
| 74 | extra_output_guardrails: list[Any] | None = None, |
| 75 | ) -> P | Paper: |
| 76 | """Extract a ``Paper`` from a typed ``PaperInput``. |
| 77 | |
| 78 | See design doc §4.1 for the rationale on each kwarg. ``memory`` is a |
| 79 | PR6 placeholder — non-None values are accepted but unused in PR5. |
| 80 | |
| 81 | Raises: |
| 82 | UnsupportedContentTypeError: When fetched bytes are not PDF / |
| 83 | HTML / markdown / plain-text. |
| 84 | NotImplementedError: When ``input`` is a ``DoiIdentifier`` (the |
| 85 | unpaywall fallback is its own follow-up issue). |
| 86 | """ |
| 87 | cfg = cfg or PaperFlowCfg() |
| 88 | out_type: type[Paper] = output_type or Paper # type: ignore[assignment] |
| 89 | |
| 90 | raw_md, source_meta = await _fetch_and_format(input) |
| 91 | |
| 92 | # Agent's `model_settings` parameter is non-optional (defaults to a |
| 93 | # fresh ``ModelSettings()``); only forward when cfg has one set. |
| 94 | agent_kwargs: dict[str, Any] = { |
| 95 | "name": "paper_extractor", |
| 96 | "instructions": _compose_instructions( |
| 97 | _DEFAULT_INSTRUCTIONS, extra_instructions, cfg |
| 98 | ), |
| 99 | "model": cfg.model, |
| 100 | "tools": list(extra_tools or []), |
| 101 | "output_type": out_type, |
| 102 | "input_guardrails": list(extra_input_guardrails or []), |
| 103 | "output_guardrails": list(extra_output_guardrails or []), |
| 104 | } |
| 105 | if cfg.model_settings is not None: |
| 106 | agent_kwargs["model_settings"] = cfg.model_settings |
| 107 | agent: Agent[Any] = Agent(**agent_kwargs) |
| 108 | return await run_with_observability( |
| 109 | agent, |
| 110 | _format_input(raw_md, source_meta), |
| 111 | cfg=cfg, |
| 112 | memory=memory, |
| 113 | extra_run_hooks=list(extra_run_hooks or []), |
| 114 | ) |
| 115 | |
| 116 | |
| 117 | async def _fetch_and_format( |