Skip to content

feat(cli): add Agent-friendly output flags (-q, -f json, --with-sources) to query command - #2

Open
als3453 wants to merge 1 commit into
RaghavOG:mainfrom
als3453:feat/cli-query-agent-friendly
Open

feat(cli): add Agent-friendly output flags (-q, -f json, --with-sources) to query command#2
als3453 wants to merge 1 commit into
RaghavOG:mainfrom
als3453:feat/cli-query-agent-friendly

Conversation

@als3453

@als3453 als3453 commented Aug 19, 2026

Copy link
Copy Markdown

Summary

Adds 3 new flags to the rag-python query CLI command, enabling the tool to be callable as a structured Agent tool (via subprocess + json.loads()), while remaining fully backward-compatible.

New flags

Flag Purpose
-q / --quiet Print the answer only — no evaluation / sources trailers. Script-friendly.
-f / --output-format {text,json,json-pretty} Return structured JSON {text, sources, evaluation, retried} — designed for Agent tool use.
--with-sources Print the top-5 sources trailer without the verbose evaluation section.

Example: calling as an Agent tool

import json, subprocess
out = subprocess.check_output([
    "rag-python", "query", "What is the PTO policy?",
    "-f", "json", "--quiet", "--embedding-provider", "local"
])
answer = json.loads(out)
print(answer["text"])
print([s["metadata"].get("source") for s in answer["sources"]])

Why this change (motivation)

Currently, Agent orchestrators (LangChain / CrewAI / custom ReAct loops) that want to wrap rag-python as a Tool / Function Calling payload have to:

  1. Parse human-printed text (regex-heavy and brittle), or
  2. Write their own wrapper script — duplicating the provider/search argument surface.

This PR turns the CLI into a first-class structured output endpoint with 0 breaking changes to the default user experience.

Testing

Added 8 hermetic unit tests under tests/test_cli_query_agent_output.py:

  • A1--help lists all 3 new flags and their short aliases
  • A2--quiet prints the answer only (no trailers)
  • A3-q short alias equals --quiet
  • A4-f json produces valid JSON with 4 keys + full sources list
  • A5-f json-pretty produces valid, indent=2 pretty-printed JSON
  • A6--with-sources prints sources trailer without requiring -v
  • A7 — default text output + -v verbose behavior unchanged (backward compat)
  • A8 — streaming with --quiet emits tokens only; no trailers after

Plus regression checks (--version, docs quickstart, docs --list) all pass.

Full TDD: RED (8/8 failed on unmodified main) → GREEN (8/8 pass after this change) → REFACTOR (0 ruff line-length / unused-import violations).

Checklist

  • Code follows the project's style (uses same dataclasses.replace pattern for search, ruff-clean)
  • New tests pass
  • Backward-compatible (default rag-python query "..." unchanged)
  • Help text updated with Agent usage examples in the query epilog
  • No new dependencies (stdlib only: argparse, json)

Closes # — (no matching issue; this is a UX/toolability improvement)

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