Skip to content

LLM Integration #12

Open
Open

Description

@liamca

As a feature suggestion, I would love if this could allow you to plug in an LLM (such as GPT-4o) where images that are included in the content could be sent to the LLM for further understanding. For example, if it was say a bar chart or graph, it would be sent to convert that to a Markdown table and be integrated in the final Markdown output.

Activity

  1. added
    enhancement
    good first issue
    on Dec 13, 2024
  2. dolbex commented on Dec 13, 2024

    @dolbex

    I could see something coming back giving us markers of where that content should appear along with the source content in a way that we could handle it as we please. Ex: Send to LLM, upload to a server and link, etc.

  3. janzheng commented on Dec 13, 2024

    @janzheng

    maybe as an MCP server? That wouldn't be too hard

  4. gagb commented on Dec 13, 2024

    @gagb
    Contributor
  5. gagb commented on Dec 13, 2024

    @gagb
    Contributor

    As a feature suggestion, I would love if this could allow you to plug in an LLM (such as GPT-4o) where images that are included in the content could be sent to the LLM for further understanding. For example, if it was say a bar chart or graph, it would be sent to convert that to a Markdown table and be integrated in the final Markdown output.

    It's already supported, here you go. You need an openai key.

    from markitdown import MarkItDown
    from openai import OpenAI
    
    client = OpenAI()
    md = MarkItDown(mlm_client=client, mlm_model="gpt-4o")
    image_file = 'https://gagb.github.io/imgs/bansal-chi21.png'
    result = md.convert(source=image_file)
    print(result.text_content)

    Output

    ImageSize: 388x368
    
    # Description:
    This image illustrates a comparison of decision accuracy with and without explanations. On the left, there's a computer icon connected to a user symbol, suggesting human-computer interaction. The vertical axis represents the "Accuracy of decisions," ranging from 0.0 to 1.0.
    
    Two dotted lines extend horizontally: one labeled "R only" and another higher up labeled "R+Explanation." A red arrow points upward from "R only" to "R+Explanation," indicating an increase in accuracy with the addition of explanations. The red delta symbol (∆) signifies the improvement, which is greater than zero. A blue question mark next to it suggests uncertainty or inquiry related to this improvement.
    
    At the bottom, the label "B Ours" signifies that this concept is part of the author's work or findings. Overall, the diagram emphasizes the value of explanations in enhancing decision-making accuracy in human-computer interactions.
  6. sdhutchins commented on Dec 13, 2024

    @sdhutchins

    Gagan Bansal (@gagb) Would be great to have this as an example in the README! Thanks.

  7. liamca commented on Dec 14, 2024

    @liamca
    Author

    @gagb Would be great to have this as an example in the README! Thanks.

    Agreed. IMO, a PDF based example would be best where it is able to do the text in the normal way, but any images in the PDF are sent to the LLM. I think this is a more compelling example to organizations then just PNG's.

  8. alejlatorre commented on Dec 14, 2024

    @alejlatorre

    As a feature suggestion, I would love if this could allow you to plug in an LLM (such as GPT-4o) where images that are included in the content could be sent to the LLM for further understanding. For example, if it was say a bar chart or graph, it would be sent to convert that to a Markdown table and be integrated in the final Markdown output.

    It's already supported, here you go. You need an openai key.

    from markitdown import MarkItDown
    from openai import OpenAI

    client = OpenAI()
    md = MarkItDown(mlm_client=client, mlm_model="gpt-4o")
    image_file = 'https://gagb.github.io/imgs/bansal-chi21.png'
    result = md.convert(source=image_file)
    print(result.text_content)
    Output

    ImageSize: 388x368

    Description:

    This image illustrates a comparison of decision accuracy with and without explanations. On the left, there's a computer icon connected to a user symbol, suggesting human-computer interaction. The vertical axis represents the "Accuracy of decisions," ranging from 0.0 to 1.0.

    Two dotted lines extend horizontally: one labeled "R only" and another higher up labeled "R+Explanation." A red arrow points upward from "R only" to "R+Explanation," indicating an increase in accuracy with the addition of explanations. The red delta symbol (∆) signifies the improvement, which is greater than zero. A blue question mark next to it suggests uncertainty or inquiry related to this improvement.

    At the bottom, the label "B Ours" signifies that this concept is part of the author's work or findings. Overall, the diagram emphasizes the value of explanations in enhancing decision-making accuracy in human-computer interactions.

    Hi! Maybe I did not find the way and is already implemented but that looks more like the description of the image rather than the text extraction for example. Is there any way to customize the prompt?

  9. Soulter commented on Dec 16, 2024

    @Soulter
    Contributor

    As a feature suggestion, I would love if this could allow you to plug in an LLM (such as GPT-4o) where images that are included in the content could be sent to the LLM for further understanding. For example, if it was say a bar chart or graph, it would be sent to convert that to a Markdown table and be integrated in the final Markdown output.

    It's already supported, here you go. You need an openai key.
    from markitdown import MarkItDown
    from openai import OpenAI
    client = OpenAI()
    md = MarkItDown(mlm_client=client, mlm_model="gpt-4o")
    image_file = 'https://gagb.github.io/imgs/bansal-chi21.png'
    result = md.convert(source=image_file)
    print(result.text_content)
    Output
    ImageSize: 388x368

    Description:

    This image illustrates a comparison of decision accuracy with and without explanations. On the left, there's a computer icon connected to a user symbol, suggesting human-computer interaction. The vertical axis represents the "Accuracy of decisions," ranging from 0.0 to 1.0.
    Two dotted lines extend horizontally: one labeled "R only" and another higher up labeled "R+Explanation." A red arrow points upward from "R only" to "R+Explanation," indicating an increase in accuracy with the addition of explanations. The red delta symbol (∆) signifies the improvement, which is greater than zero. A blue question mark next to it suggests uncertainty or inquiry related to this improvement.
    At the bottom, the label "B Ours" signifies that this concept is part of the author's work or findings. Overall, the diagram emphasizes the value of explanations in enhancing decision-making accuracy in human-computer interactions.

    Hi! Maybe I did not find the way and is already implemented but that looks more like the description of the image rather than the text extraction for example. Is there any way to customize the prompt?

    It can customize prompt, just pass the mlm_prompt to convert method.

    Like this:

    # ...
    prompt = "extract the objects in the picture, and only output keywords, separated by commas."
    result = markitdown.convert("path/to/example.jpg", mlm_prompt=prompt)

    But the latest version cannot apply the param properly when trying to convert an url file, and I just created a pr to fix it. #48

  10. vaclcer commented on Dec 18, 2024

    @vaclcer

    @gagb Would be great to have this as an example in the README! Thanks.

    Agreed. IMO, a PDF based example would be best where it is able to do the text in the normal way, but any images in the PDF are sent to the LLM. I think this is a more compelling example to organizations then just PNG's.

    Exactly this. We could use something like PyMuPDF4LLM to extract images and classify them with VLM later, but if this could be done in one step during pdf->md conversion, that would be brilliant!

  11. tinosai commented on Dec 18, 2024

    @tinosai

    Agreed. IMO, a PDF based example would be best where it is able to do the text in the normal way, but any images in the PDF are sent to the LLM. I think this is a more compelling example to organizations then just PNG's.

    Completely agree.
    Basically, in pseudocode:

    take in a PDF
    go through it and for each image
        send the image to an LLM for semantic understanding/text parsing (task TBD by the user)
        replace the LLM output in the markdown (inline)
    

    This way, the tool would be really useful, and the last issue to be resolved would be proper table parsing (from pdf)

  12. gagb commented on Dec 18, 2024

    @gagb
    Contributor

    Fortunato Nucera (@tinosai) Lets move the pdf specific ideas to #131

  13. raffaeler commented on Dec 25, 2024

    @raffaeler

    In general I would love the ability to use hooks when converting any document containing images.
    Every time the converter finds an image, call me back with the byte[] so that I can handle the converstion of the image to text by myself. This may also happen with a local model and not just GPT.

    The point is replacing the images with their description in any type of document. I mean that replacement implies keeping the sequence of text/image parts.

  14. liamca commented on Jan 7, 2025

    @liamca
    Author

    Agreed. IMO, a PDF based example would be best where it is able to do the text in the normal way, but any images in the PDF are sent to the LLM. I think this is a more compelling example to organizations then just PNG's.

    Completely agree. Basically, in pseudocode:

    take in a PDF
    go through it and for each image
        send the image to an LLM for semantic understanding/text parsing (task TBD by the user)
        replace the LLM output in the markdown (inline)
    

    This way, the tool would be really useful, and the last issue to be resolved would be proper table parsing (from pdf)

    If I could make one additional suggestion to this excellent pseudocode, it would be to include the ability to get the page number. Very likely this would be possible with the pseudocode, but I point it out because of the importance of this from a citation perspective. When doing RAG, you want to allow a user to go directly in the document to where the answer was generated. Since markdown does not have a concept of page numbers, you need to be able to let them go to something like the original PDF, and having the page number makes that a lot more efficient.

  15. raffaeler commented on Jan 7, 2025

    @raffaeler

    If I could make one additional suggestion to this excellent pseudocode, it would be to include the ability to get the page number.

    Liam Cavanagh (@liamca) You should never need that. You can store the page and even more details in the payload that you then associate to the embedding.

    Embeddings should never go further than a page and in many cases the amount of text is way too much to get decent results.

  16. tinosai commented on Jan 7, 2025

    @tinosai

    I have sketched some of the code that takes care of passing the image to the LLM from the original PDF.
    However, the main problem is that in many circumstances pdfminer is unable to properly parse images in the document.

    We need to replace pdfminer IMHO.

  17. AjjayK commented on May 1, 2025

    @AjjayK

    Agreed. IMO, a PDF based example would be best where it is able to do the text in the normal way, but any images in the PDF are sent to the LLM. I think this is a more compelling example to organizations then just PNG's.

    Completely agree. Basically, in pseudocode:

    take in a PDF
    go through it and for each image
        send the image to an LLM for semantic understanding/text parsing (task TBD by the user)
        replace the LLM output in the markdown (inline)
    

    This way, the tool would be really useful, and the last issue to be resolved would be proper table parsing (from pdf)

    Perfect case why we need this feature. As an example, let's look at this slide from a PDF. This md extraction is no where a representation of what the slide conveys

    Image

    ### MarkItDown's Output
    Crompton 2.0 continues to deliver results: Sustaining growth rates across ECD and Lighting
    segments; Strong margin expansion in ECD; Profitability remains intact even with higher A&P
    spends in Lighting
    
    Q3 FY25 ECD revenue grew by 6% YoY;
    EBIT rose 19% YoY to 196 Cr.
    
    Q3 FY25 lighting revenue grew by 3% YoY to Rs. 257 Cr,
    demonstrating continued momentum
    
    Standalone ECD revenue (Rs. Cr)
    
    Standalone lighting revenue (Rs. Cr)
    
    14%
    
    4,407
    
    3,876
    
    6%
    
    1,288
    
    1,209
    
    4%
    
    743
    
    716
    
    3%
    
    257
    
    249
    
    9M FY24
    
    9M FY25
    
    Q3 FY24
    
    Q3 FY25
    
    9M FY24
    
    9M FY25
    
    Q3 FY24
    
    Q3 FY25
    
    ECD EBIT (Rs. Cr) & EBIT Margin
    
    Lighting EBIT (Rs. Cr) & EBIT Margin
    
    661
    
    15.0%
    
    27%
    
    521
    
    13.5%
    
    19%
    
    196
    15.2%
    
    164
    13.6%
    
    A&P
    Spends
    
    EBIT
    
    5.5
    
    80
    
    11.2%
    
    23.3
    
    76
    
    10.2%
    
    3.2
    
    28
    
    8.0
    
    28
    
    11.2%
    
    10.8%
    
    9M FY24
    
    9M FY25
    
    Q3 FY24
    
    Q3 FY25
    
    Note: Standalone Financials
    
    9M FY24
    
    9M FY25
    
    Q3 FY24
    
    Q3 FY25
    
    6
    
    
    
    
  18. IgnazioDS commented on Jun 8, 2026

    @IgnazioDS

    For anyone building RAG pipelines that ingest mixed-content PDFs, the image→LLM→inline-markdown pattern is very useful in production. A few patterns worth sharing:

    Using Claude instead of GPT-4o for image understanding:

    import anthropic
    from markitdown import MarkItDown
    
    # Wrap the Anthropic client to match the OpenAI-compatible interface MarkItDown expects
    class AnthropicMLMClient:
        def __init__(self):
            self.client = anthropic.Anthropic()
        
        def chat_completions_create(self, model, messages, **kwargs):
            # adapt from OpenAI format to Anthropic format
            # ...
            pass
    
    # Or use the native mlm_client if it supports Anthropic protocol

    The mlm_prompt parameter already solves the "description vs. extraction" question — you can specify "Convert this chart to a Markdown table with exact values" vs. the default descriptive output.

    Page-level citation metadata:

    For RAG use cases, the chunk metadata matters as much as the chunk content. The suggestion about page numbers is important — when indexing into pgvector (or any vector store), you want each chunk tagged with at minimum {file, page, element_type} so the retrieval layer can surface the exact source location. Right now markitdown's output is a single flat string — a structured output mode (list of {page, content, element_type}) would make it much more RAG-friendly downstream.

    Is there an existing issue tracking structured/segmented output as a feature request? If not, happy to open one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions