# MarkupSafe — card for AI agents

Utility for safely escaping and marking HTML/XML strings to prevent injection attacks.

Category: Auth and security libraries · PyPI package `markupsafe` · latest version 3.0.4 (checked 2026-11-01)
Install: `pip install markupsafe`
License: BSD-3-Clause (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Numbers (AgentGid data)
- Gid Score: 66.7 (#566 of all tools, 18 of 141 in its category)
- Used in 45 public projects set up for AI coding agents (0.7%)
- Downloads last week: 198,398,661 (as of 2026-11-24), +15.2% over 4 weeks
- GitHub stars: 702 (as of 2026-11-24)
- Last push: 2026-10-03

## Often used with
Requests 86.7%, Click 84.4%, urllib3 82.2%, charset-normalizer 80.0%, Jinja2 80.0%, Pydantic 77.8%

## Links
- AgentGid page: https://agentgid.com/tools/markupsafe/
- Repository: https://github.com/pallets/markupsafe


## MarkupSafe: show user text on web pages safely

MarkupSafe is a small free Python library that makes text safe to put into a web page. Characters like < and & in a user's name or comment are turned into harmless codes, so a hidden script shows up as plain text instead of running. Jinja2 and Flask use it under the hood; it does not clean up HTML that you want to keep.

### Where to use it
- Show names and comments from users: A visitor signs up with a name that contains a script tag; your page shows the name as typed, and the script never runs.
- Build HTML snippets in Python code: Your app builds a small piece of HTML, like a comment card or an email line, in Python; the template is trusted and every value put into it is escaped.
- Objects that draw themselves as HTML: A User or Image class in your app gives its own HTML for templates, while the parts that come from users are still escaped.
- Plain-text previews of HTML: For a search snippet or a notification, your app turns stored safe HTML into plain words without the tags.
- Safe text in your own small template helpers: If your project prints HTML from Python without a template engine, for example in an admin script or a report generator, one escape call per value keeps it safe. (idea, not from the official pages)

### How to start
- Install it into your project's Python environment (Jinja2 and Flask already bring it along).

```
pip install -U MarkupSafe
```

- Escape any value before it goes into HTML; the result is marked safe so it is not escaped twice.

```
>>> from markupsafe import escape
>>> hello = escape("<em>Hello</em>")
>>> hello
Markup('&lt;em&gt;Hello&lt;/em&gt;')
```


### License
Free to use in personal and paid products under the BSD-3-Clause license. Keep the copyright notice and license text with copies, and do not use the project's name to promote your product.

### Good to know
- Wrapping text in Markup tells the library the text is already safe, so it is not escaped. Never do that with text from users.
- It escapes all text; it does not keep some tags and remove others. To allow bold or links in user posts, use an HTML cleaner such as nh3.
- Since version 3.0, a few text methods such as strip and partition no longer escape what you pass to them.
- It needs Python 3.9 or newer. Version 3.0.4 came out on 2026-10-02, from the Pallets team behind Flask and Jinja.

Checked 2026-11-11 against: https://markupsafe.palletsprojects.com/en/stable/, https://markupsafe.palletsprojects.com/en/stable/escaping/, https://markupsafe.palletsprojects.com/en/stable/html/, https://markupsafe.palletsprojects.com/en/stable/formatting/, https://markupsafe.palletsprojects.com/en/stable/changes/, https://markupsafe.palletsprojects.com/en/stable/license/, https://github.com/pallets/markupsafe/blob/main/README.md, https://github.com/pallets/markupsafe/blob/main/LICENSE.txt, https://pypi.org/pypi/markupsafe/json, https://pypi.org/pypi/markupsafe/3.0.4/json

### Mistakes to avoid
- Don't: Markup(user_input) or Markup(f"<b>{name}</b>") → Do: Markup("<b>{}</b>").format(name), or escape(name) (The Markup constructor marks text safe without escaping, so the user's tags would run.)
- Don't: escape(str(value)) on a value that is already Markup → Do: escape(soft_str(value)) or keep the Markup object as it is (str() drops the safe mark and the text is escaped again; our run turned & into &amp;amp;.)
- Don't: Putting card.striptags() straight back into HTML → Do: Escape the result again before it goes into a page; striptags returns plain text with HTML codes turned back into characters (In our run striptags gave back '<img src=x onerror=alert(1)>' as raw characters, because it unescapes first.)
- Don't: from markupsafe import soft_unicode → Do: from markupsafe import soft_str (soft_unicode was removed in 2.1.0; old Jinja2 versions fail on import because of it.)
- Don't: markupsafe.__version__ to check the version → Do: importlib.metadata.version("markupsafe") (The attribute is deprecated since 3.0.0 and raises a DeprecationWarning since 3.0.3.)

### Questions
- Why do I see &amp;amp; on my page? The text was escaped twice. Converting Markup back with str() drops its safe mark; keep the Markup object or use soft_str.
- Why does my page show the word None? escape(None) gives the text "None"; use escape_silent for values that may be missing.
- My old code imports soft_unicode and fails. What changed? soft_unicode was removed in version 2.1.0; use soft_str instead.
- Can I use it in a paid product? Yes. The BSD-3-Clause license allows it; keep the notice and do not use the project's name to promote your product.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
