---
title: Python module
description: Parse llms.txt files into structured Python objects
---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Given an `llms.txt` file, this provides a minimal Python API to parse it into a structured object. The input file should follow this format:

```
# FastHTML

> FastHTML is a python library which...

Base URL: https://host/

When writing FastHTML apps remember to:

- Thing to remember

## Docs

- [Surreal](README.md): Tiny jQuery alternative with Locality of Behavior
- [FastHTML quick start](quickstart.md): An overview of FastHTML features

## Examples

- [Todo app](adv_app.py)

## Reference

- [Starlette docs](https://other.example/starlette-sml.md): A subset of the Starlette docs
```

## Install

```sh
pip install llms-txt
```

## How to use

```python
from llms_txt import *
```

```python
samp = Path('llms-sample.txt').read_text()
```

Use [`parse_llms_file`](core.md#parse_llms_file) to create a data structure with the `title`, `summary`, `base_url`, `info`, and `sections` of an `llms.txt` file:

```python
parsed = parse_llms_file(samp)
list(parsed)
```

```output
['title', 'summary', 'info', 'sections']
```

```python
parsed.title,parsed.summary
```

```output
('FastHTML',
 'FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore\'s `FT` "FastTags" into a library for creating server-rendered hypermedia applications.')
```

```python
list(parsed.sections)
```

```output
['Docs', 'Examples', 'Optional']
```

```python
parsed.sections.Reference[0]
```

```json
{ 'desc': 'A subset of the Starlette documentation useful for FastHTML '
          'development.',
  'title': 'Starlette full documentation',
  'url': 'https://gist.githubusercontent.com/jph00/809e4a4808d4510be0e3dc9565e9cbd3/raw/9b717589ca44cedc8aaf00b2b8cacef922964c0f/starlette-sml.md'}
```

### Implementation and tests

To show how simple it is to parse `llms.txt` files, here's a complete parser in \<20 lines of code with no dependencies:

```python
import re,itertools

def chunked(it, chunk_sz):
    it = iter(it)
    return iter(lambda: list(itertools.islice(it, chunk_sz)), [])

def parse_llms_txt(txt):
    "Parse llms.txt file contents in `txt` to a `dict`"
    def _p(links):
        link_pat = r'-\s*\[(?P<title>[^\]]+)\]\((?P<url>[^\)]+)\)(?::\s*(?P<desc>.*))?'
        return [m.groupdict() for l in re.split(r'\n+', links.strip()) if (m:=re.search(link_pat, l))]

    start,*rest = re.split(r'^##\s+(.*?$)', txt, flags=re.MULTILINE)
    sects = {k: _p(v) for k,v in dict(chunked(rest, 2)).items()}
    pat = r'^#\s*(?P<title>.+?$)(?:\n+^>\s*(?P<summary>.+?$)$)?(?:\n+^Base URL:\s*(?P<base_url>https?://\S+)[ \t]*$)?(?:\n+(?P<info>.*))?'
    d = re.search(pat, start.strip(), (re.MULTILINE|re.DOTALL)).groupdict()
    if d['base_url']: d['base_url'] = f"{d['base_url'].rstrip('/')}/"
    d['sections'] = sects
    return d
```

We have provided a test suite in `tests/test-parse.py` and confirmed that this implementation passes all tests.
