Skip to content

fix(sdk): parse --no_ negation flags for boolean CLI parameters - #1963

Open
tomwangcn wants to merge 2 commits into
openlit:mainfrom
tomwangcn:fix/cli-bool-negation-flags
Open

tomwangcn wants to merge 2 commits into
openlit:mainfrom
tomwangcn:fix/cli-bool-negation-flags

Conversation

@tomwangcn

@tomwangcn tomwangcn commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1962

Problem

openlit-instrument --no_capture_message_content fails with error: unrecognized arguments, even though PARAMETER_CONFIG documents negation support via has_negation: True for that parameter.

Two root causes in parse_arguments:

  1. Boolean parameters are registered with a bare store_true action, so the --no_<name> form is never registered at all.
  2. store_true's implicit False default makes an unset flag indistinguishable from an explicit False, which forced the skip False when config default is True special case in set_environment_from_cli_args - and made it impossible to ever pass an explicit false for a default-True parameter.

Fix

  • Boolean flags are registered with default=None, so omission stays None and is distinguishable from an explicit value.
  • Parameters with has_negation: True in PARAMETER_CONFIG additionally get a --no_<name> flag (store_false, same dest).
  • The now-dead skip special case in set_environment_from_cli_args is removed. Semantics are now uniform:
    • flag omitted -> nothing exported -> the config default applies
    • --capture_message_content -> env var "true"
    • --no_capture_message_content -> env var "false"
    • an env var that is already set always wins (unchanged precedence)

Tests

New sdk/python/tests/test_cli_args.py (6 tests):

  • --no_capture_message_content parses to False (previously SystemExit 2)
  • --capture_message_content parses to True
  • omitted boolean flags stay None
  • explicit --no_ exports "false" to the env var
  • omitted flags export nothing
  • a pre-set env var takes precedence over the CLI flag

Verified red on current main (5 of 6 fail), green with the fix. Also re-ran the existing sdk/python/tests/test_env_config.py: 18 passed / 25 errors both before and after this change (the 25 errors are pre-existing import-time failures in my sandbox due to missing optional instrumentation dependencies, not related to this change; CI covers the full environment).

$ openlit-instrument --no_capture_message_content python app.py
# before: error: unrecognized arguments: --no_capture_message_content
# after:  OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=false

Summary by Sourcery

Fix boolean CLI configuration so negation flags are accepted and explicit values are propagated without overriding existing environment settings.

Bug Fixes:

  • Support positive and --no_ negation forms for documented boolean CLI parameters, including explicit false values.

Enhancements:

  • Preserve omitted boolean flags as unset so configuration defaults continue to apply consistently while explicit CLI values are exported.

Tests:

  • Add CLI coverage for boolean flag parsing, omission behavior, negation export, conflicting flags, and environment-variable precedence.

Boolean CLI parameters were registered with a bare store_true action, so
the documented --no_capture_message_content flag was unrecognized and the
implicit False default made an unset flag indistinguishable from an
explicit False.

- Register boolean flags with default=None so omission stays None
- Register --no_<name> (store_false, same dest) for params with
  has_negation set in PARAMETER_CONFIG
- Drop the now-dead skip-False-when-default-True special case in
  set_environment_from_cli_args; explicit False now exports 'false',
  omission exports nothing, and env vars still take precedence

Fixes openlit#1962
@tomwangcn
tomwangcn requested review from a team and ishanjainn as code owners November 16, 2026 19:50
@sourcery-ai

sourcery-ai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Register boolean flags with explicit unset values and configured negation forms, and export explicit false values instead of suppressing them.

  • sdk/python/src/openlit/cli/main.py L67-87, L125 (removed lines)

Tests

Add tests for positive, negative, and omitted boolean flags, environment propagation and precedence, and rejection of conflicting flags.

  • sdk/python/tests/test_cli_args.py L1-80

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Fix negated boolean flags in the Python SDK CLI

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Recognize configured --no_ flags so users can explicitly disable default-true options.
• Keep omitted boolean flags unset while preserving existing environment-variable precedence.
• Add tests for parsing, environment export, omission, and precedence.
Diagram

graph TD
  CFG["Parameter config"] --> PARSER["CLI parser"] --> PROVIDED{"Flag supplied?"}
  PROVIDED -- "No" --> KEEP["Use existing/default"] --> RUNTIME["Runtime config"]
  PROVIDED -- "Yes" --> PRESET{"Env already set?"}
  PRESET -- "Yes" --> KEEP
  PRESET -- "No" --> EXPORT["Export true/false"] --> RUNTIME
Loading
High-Level Assessment

The configuration-driven approach fits the existing CLI: it exposes negation only where declared and uses None to distinguish omission from an explicit false value. A blanket boolean-negation action would change the accepted flags for other parameters.

Files changed (2) +68 / -5

Bug fix (1) +13 / -5
main.pyParse configured negation flags without exporting omitted booleans +13/-5

Parse configured negation flags without exporting omitted booleans

• Boolean arguments now default to None, and parameters marked for negation accept a matching '--no_<name>' flag. Environment export no longer skips explicit false values for default-true parameters; pre-existing environment values still win.

sdk/python/src/openlit/cli/main.py

Tests (1) +55 / -0
test_cli_args.pyCover boolean flag parsing and environment precedence +55/-0

Cover boolean flag parsing and environment precedence

• Adds six tests for positive, negated, and omitted flags, plus explicit-false export, omission, and precedence of a pre-set environment variable.

sdk/python/tests/test_cli_args.py

@sourcery-ai

sourcery-ai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit a9ff671

#1962 Bug: Python CLI rejects configured --no_capture_message_content option · 4 of 5 met

# Requirement Status Notes
1 Accept --no_capture_message_content and parse it as false. ✅ Met The parser registers the negative option with store_false.
2 If OPENLIT_CAPTURE_MESSAGE_CONTENT is not already set, export false to the launched process. 🟡 Partly met False is exported, but to OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, not the environment variable named in the issue.
3 Preserve existing environment-variable precedence. ✅ Met CLI values are only exported when the configured environment variable is not already present.
4 Preserve omitted-flag defaults and other boolean flags. ✅ Met Omitted booleans remain None and are not exported; boolean flags without negation remain supported.
5 Reject specifying both positive and negative forms. ✅ Met The positive and negative forms are registered in a mutually exclusive group.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="sdk/python/src/openlit/cli/main.py" line_range="69-79" />
<code_context>
-            parser.add_argument(cli_arg, action="store_true", help=cli_help)
+            # default=None (instead of store_true's implicit False) so that an
+            # unset flag is distinguishable from an explicit False.
+            parser.add_argument(
+                cli_arg, action="store_true", default=None, help=cli_help
+            )
+            if config.get("has_negation"):
+                parser.add_argument(
</code_context>
<issue_to_address>
**Reject conflicting flags**

The issue requires rejecting commands that specify both positive and negative forms. The change registers both actions independently, so conflicting forms are accepted and the last one wins.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: sdk/python/src/openlit/cli/main.py:79


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread sdk/python/src/openlit/cli/main.py Resolved
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Both capture flags accepted together ✓ Resolved

Resolving commit: a9ff671

Resolution reason
The positive and negative flags are now registered in an argparse mutually exclusive group (sharing
dest and default=None) when has_negation is set, so argparse rejects specifying both. Added a test
that passes both orders and asserts SystemExit. This matches the suggested fix.
Description
parse_arguments registers -- and --no_ as independent actions on the same destination, so
argparse keeps the last value instead of rejecting the pair. When both forms are supplied for a
has_negation boolean, their order determines the setting exported to the launched application, and
the new tests cover only single-flag cases.

Grey Divider

Tip of the day
💡 Did you know, you can choose which labels appear on a finding, and whether they show icons or text

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Register the --<name>/--no_<name> pair in a mutually exclusive group so
passing both exits with an argparse error instead of silently keeping
the last one, as the issue's expected behavior requires.
@tomwangcn

Copy link
Copy Markdown
Contributor Author

Both review bots flagged that the positive and negative forms could be passed together, with the last one silently winning. The issue's expected behavior does require rejecting that combination, so the pair is now registered in a mutually exclusive group (a9ff671): passing both --capture_message_content and --no_capture_message_content exits with an argparse error in either order, covered by a new test_conflicting_flags_rejected case (7/7 tests green, verified red before the change).

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

New gaps against the linked issues

Comment thread sdk/python/src/openlit/cli/main.py Resolved

This branch was previously deployed

1 inactive (outdated) deployment
release-notes — 1563b249 Deployed Nov 16, 2026 by tomwangcn via Normalize / Conventional title #844
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Python CLI rejects configured --no_capture_message_content option

1 participant