DSPy: The framework for programming—not prompting—language models
Python
Fix Document.format() using text source type for PDFs (#9977)
Nishchay Mahorb28682b97264
diff --git a/dspy/adapters/types/document.py b/dspy/adapters/types/document.pyindex 145f5eb90..06f3b0a00 100644--- a/dspy/adapters/types/document.py+++ b/dspy/adapters/types/document.py@@ -64,10 +64,12 @@ class Document(Type):Returns:A list containing the document block in the format expected by citation-enabled language models."""+ # PDFs use a base64 source; only text/plain uses a text source.+ source_type = "base64" if self.media_type == "application/pdf" else "text"document_block = {"type": "document","source": {- "type": "text",+ "type": source_type,"media_type": self.media_type,"data": self.data},diff --git a/tests/adapters/test_document.py b/tests/adapters/test_document.pyindex 98c714440..e4ae3cbe5 100644--- a/tests/adapters/test_document.py+++ b/tests/adapters/test_document.py@@ -55,3 +55,17 @@ def test_document_format():assert doc_block["source"]["data"] == "The sky is blue."assert doc_block["title"] == "Color Facts"assert doc_block["citations"]["enabled"] is True+++def test_document_format_pdf_uses_base64_source():+ doc = Document(+ data="base64pdfdata",+ media_type="application/pdf",+ )++ doc_block = doc.format()[0]++ # PDF documents must use a base64 source, not a text source.+ assert doc_block["source"]["type"] == "base64"+ assert doc_block["source"]["media_type"] == "application/pdf"+ assert doc_block["source"]["data"] == "base64pdfdata"