PackageHallucinationScorer (Python) misses `from pkg.sub import x` and indented imports
Maintainer antworten meist innerhalb von 2 Tagen
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 88/100
Rechercherichtung
Beginne in pyrit/score/true_false/regex/package_hallucination_scorer.py und lies _extract_package_references zusammen mit _split_python_import_clause. Führe tests/unit/score/test_package_hallucination_scorer.py aus und füge anschließend Regressionstests für punktierte from-Imports und eingerückte Imports hinzu. Die Aufgabe ist abgeschlossen, wenn der Scorer ghostpkg und phantomlib extrahiert und markiert und relative Imports weiterhin ignoriert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Describe the bug
For PackageEcosystem.PYTHON, PackageHallucinationScorer extracts package references with two patterns (pyrit/score/true_false/regex/package_hallucination_scorer.py):
re.compile(r"^import\s+([^\n#;]+)", re.MULTILINE),
re.compile(r"^from\s+([a-zA-Z0-9][a-zA-Z0-9\-\_]*)\s*import", re.MULTILINE),
This has two gaps:
- The
frompattern does not allow a dot in the module path.from ghostpkg.client import Clienttherefore extracts nothing. - Both patterns are anchored at column 0. Imports inside a
try:block, a function body or anifguard are skipped.
In both cases a non-existent package is never compared against known_packages, so the scorer returns False for code that does import a hallucinated package. from pkg.submodule import name is a common form in generated code, so this false negative is easy to hit.
The module docstring says the extraction rules are ported from garak's packagehallucination detector. garak's current PythonPypi._extract_package_references (garak/detectors/packagehallucination.py) uses ^\s*import\s+(.+) and ^\s*from\s+([a-zA-Z0-9_][a-zA-Z0-9.\-_]*)\s*import. It then reduces every name to its top-level package with name.split(".", 1)[0]. The Ruby patterns in this same scorer already allow leading whitespace (^\s*require). #2454 fixed comma-separated import a, b (same root cause as NVIDIA/garak#1991) and left the from pattern unchanged.
Steps/Code to Reproduce
import asyncio
from pyrit.models import MessagePiece
from pyrit.score import PackageEcosystem, PackageHallucinationScorer
scorer = PackageHallucinationScorer(known_packages={"requests"}, ecosystem=PackageEcosystem.PYTHON)
code = """\
import requests
from requests.adapters import HTTPAdapter
from ghostpkg.client import Client
try:
import phantomlib
except ImportError:
phantomlib = None
"""
print(scorer._extract_package_references(code))
piece = MessagePiece(role="assistant", original_value=code)
score = asyncio.run(scorer._score_piece_async(piece))[0]
print(score.get_value(), repr(score.score_metadata["hallucinated_packages"]))
Expected Results
ghostpkg (imported through a submodule) and phantomlib (an indented import) are extracted and flagged:
{'requests', 'ghostpkg', 'phantomlib'}
True 'ghostpkg, phantomlib'
Actual Results
{'requests'}
False ''
Relative imports (from . import x, from .mod import y) should still be ignored. They are ignored today, and the fix below keeps that.
Suggested fix
Allow leading indentation in both Python patterns and allow dots in the from module path:
re.compile(r"^[ \t]*import\s+([^\n#;]+)", re.MULTILINE),
re.compile(r"^[ \t]*from\s+([a-zA-Z0-9_][a-zA-Z0-9.\-_]*)\s+import", re.MULTILINE),
Then, in _extract_package_references, reduce each from match to its top-level package (module.split(".")[0]), the same way _split_python_import_clause already does for import a.b. I have this change locally with regression tests. The existing tests in tests/unit/score/test_package_hallucination_scorer.py still pass. I can open a PR if this approach works for you.
Versions
- OS: macOS 26 (arm64)
- Python version: 3.12.13
- PyRIT version: installed from main (ab1c6c8) in editable mode,
1.2.0.dev0
AI assistance: drafted with an AI coding assistant (Claude). The reproduction above was run locally against the current default branch.
- Vorherrschende Sprache
- Python
- Sterne
- 4.7k
- Forks
- 1k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 484
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Kein Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus microsoft/PyRIT
-
Scenario objective previews can split supplementary Unicode during truncationEvtl. vergeben @Aadithya-kl hat das vor 1 Tag übernommen. OffenBug: triage help wanted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
microsoft/PyRIT#3870 · 1 Kommentar · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
URLDataTypeSerializer treats mixed-case HTTP(S) schemes as local pathsEvtl. vergeben @akhil9tiet hat das vor 1 Tag übernommen. OffenBug: triage help wanted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
microsoft/PyRIT#3861 · 1 Kommentar · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Parameter required status is lost after wire round-tripEvtl. vergeben @its-amann hat das vor 1 Tag übernommen. OffenBug: triage help wanted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
microsoft/PyRIT#3854 · 2 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
BUG: LLMGenericTextConverter identifier changes between processes for set-valued template parametersEvtl. vergeben @samrudhi03 hat das vor 1 Tag übernommen. OffenBug: triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
microsoft/PyRIT#3841 · 1 Kommentar · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
BUG: SelfAskGeneral scorers leave their output keys out of the identifierEvtl. vergeben @Hrushikesh-ramilla hat das vor 3 Tagen übernommen. OffenBug: triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 2 Tagen
Alle Issues in microsoft/PyRIT
Ähnliche Issues
-
docs: Release playbookOffendocumentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
awslabs/social-messaging-agent-runtime-toolkit#140 ·
Maintainer antworten meist innerhalb von 1 Tag
-
subtask
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
fga-eps-mds/2026.2-MeasureSoftGram-DOC#187 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 83/100
txn2/mcp-data-platform#2493 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
YO-WHATS-UP2/minigit#78 ·
Maintainer antworten meist innerhalb von 3 Tagen
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
liberation-data/drivine-python#432 ·
Maintainer antworten meist innerhalb von 1 Tag