Skip to content

Fall back to the text format for a non-numeric Accept version - #1213

Merged
csmarchbanks merged 1 commit into
prometheus:masterfrom
chrikrah:chrikrah/accept-version-non-numeric
Oct 14, 2026
Merged

csmarchbanks merged 1 commit into
prometheus:masterfrom
chrikrah:chrikrah/accept-version-non-numeric

Conversation

@chrikrah

@chrikrah chrikrah commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

choose_encoder compares the Accept version with parse_version(version) >= (1, 0, 0) (prometheus_client/exposition.py:375 and :384). With 1.0.0-rc.1, a quoted "1.0.0" or latest, that comparison raises TypeError and the scrape answers 500. This pull request treats a version with a non-numeric part as unsupported, so it falls through to the next media range and ends at text/plain; version=0.0.4, as version=1.0 already does.

Before, at 9cd073c:

$ python accept_version.py
'application/openmetrics-text; version=1.0.0-rc.1' -> raises TypeError '>=' not supported between instances of 'str' and 'int'
'application/openmetrics-text; version=latest' -> raises TypeError '>=' not supported between instances of 'str' and 'int'
'text/plain; version=1.0.0rc1' -> raises TypeError '>=' not supported between instances of 'str' and 'int'
'application/openmetrics-text; version=latest' client got HTTPError HTTP Error 500: Internal Server Error

Before #1136, packaging.Version raised InvalidVersion here for latest. Since #1136, a pre-release such as 1.0.0-rc.1 fails too.

Verification

# Python 3.14.7
$ python -m pytest -q tests
429 passed, 1 skipped, 3 subtests passed in 8.04s
# base 9cd073c: 428 passed, 1 skipped
$ git stash -- prometheus_client/exposition.py && python -m pytest -q tests/test_exposition.py   # fix reverted, test kept
3 failed, 98 passed, 1 skipped in 3.32s   # the 3 subtests of test_non_numeric_version, TypeError
$ flake8 prometheus_client/ tests/ && isort --check prometheus_client/ tests/   # flake8 7.4.1, isort 5.10.1
# not run: tox across Python versions; mypy 2.4.0 reports the same 2 errors in metrics.py on base and branch

No issue exists for this.

@csmarchbanks, should a quoted version="1.0.0" be unquoted and served as OpenMetrics 1.0.0? I can add that to this pull request.

choose_encoder compared the version parameter of the Accept header as a
tuple of mixed ints and strings, so a version such as 1.0.0-rc.1, a
quoted "1.0.0" or latest raised TypeError and the scrape answered 500.
Treat such a version as unsupported, which falls through to the next
media range and finally to text/plain version 0.0.4.

Signed-off-by: Christopher Krah <github@christopher-krah.com>

@csmarchbanks csmarchbanks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@csmarchbanks

Copy link
Copy Markdown
Member

should a quoted version="1.0.0" be unquoted and served as OpenMetrics 1.0.0

I think yes, but I do not feel strongly. I am going to merge this PR to avoid waiting, but feel free to open another PR with that change if you want.

@csmarchbanks
csmarchbanks merged commit 2aac0e0 into prometheus:master Oct 14, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants