Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 3 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

MarkupSafe

Show user text on web pages safely.

Auth and security librariesPYPI

GitHub

1. License

“Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met”

BSD-3-Clause allows use in paid and closed products.

github.com/pallets/markupsafe/blob/main/LICENSE.txt

From the package registry and GitHub, checked daily; card checked Nov 11, 2026. Tap a verdict to see its source.How we checkedReport an error

What MarkupSafe does

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.

What people build with 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 Idea

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.

MarkupSafe popularity

67 / 100
Gid Score, #566 overall
198M
downloads a week, pypi
702
GitHub stars
45
agent projects use it
MarkupSafe: downloads per weekPyPI, markupsafe 198,094,661
MarkupSafe: GitHub starspallets/markupsafe 702

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How MarkupSafe is used

  • MarkupSafe is a dependency in 45 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (2.8% of the 1,623 Python ones), #15 of 141 in auth and security libraries.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,197; those are left out of every share on this page.
  • Projects that use it most often also use Requests (86.7%), Click (84.4%), urllib3 (82.2%).

By coding agent

27 projects, 0.9%
22 projects, 0.7%
19 projects, 0.8%

Often used with

In 86.7% of its projects
In 84.4% of its projects
In 82.2% of its projects
In 80.0% of its projects
In 80.0% of its projects
In 77.8% of its projects

More about MarkupSafe

Funding
  • GitHub Sponsors: https://github.com/pallets
  • Own page: https://palletsprojects.com/donate
  • Tidelift: https://tidelift.com/funding/github/pypi/Flask
  • thanks.dev: https://thanks.dev/u/gh/pallets
  • 31 GitHub sponsors

See all

More in Auth and security libraries

Alternatives

MarkupSafe This page

BSD-3-ClauseGid 67198M/wk

nh3

Pick nh3 when users may post some HTML, like bold text and links, and you want to keep the allowed tags and remove the rest.

Jinja2

Pick Jinja2 when you are building whole pages; it escapes values for you in HTML templates, using MarkupSafe inside.

Bleach

Pick Bleach for an allow-list HTML cleaner in older Python projects that already use it.

Compare MarkupSafe with another tool

Get started

Tell your AI assistant

“In this Python code that builds HTML by hand, use MarkupSafe 3.0.4 (pin markupsafe==3.0.4) to wrap only our own fixed template strings in Markup and pass every user value through Markup.format or escape; never wrap user input in Markup.”

  1. Install it into your project's Python environment (Jinja2 and Flask already bring it along).

    pip install -U MarkupSafe

    From markupsafe.palletsprojects.com/en/stable/

  2. 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;')
    

    From markupsafe.palletsprojects.com/en/stable/

Mistakes to avoid

Markup(user_input) or Markup(f"<b>{name}</b>")
Markup("<b>{}</b>").format(name), or escape(name)

The Markup constructor marks text safe without escaping, so the user's tags would run.

escape(str(value)) on a value that is already Markup
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;.

Putting card.striptags() straight back into HTML
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.

from markupsafe import soft_unicode
from markupsafe import soft_str

soft_unicode was removed in 2.1.0; old Jinja2 versions fail on import because of it.

markupsafe.__version__ to check the version
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.