Skip to content

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

Open
chrikrah wants to merge 1 commit into
prometheus:masterfrom
chrikrah:chrikrah/accept-version-non-numeric
Open

chrikrah wants to merge 1 commit into
prometheus:masterfrom
chrikrah:chrikrah/accept-version-non-numeric

Conversation

@chrikrah

@chrikrah chrikrah commented Oct 4, 2026

Copy link
Copy Markdown

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>

This branch has not been deployed

No deployments
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.

1 participant