Skip to content

Warn when string input matches an existing file path - #489

Open
UditDewan wants to merge 4 commits into
google:mainfrom
UditDewan:warn-file-path-input
Open

Warn when string input matches an existing file path#489
UditDewan wants to merge 4 commits into
google:mainfrom
UditDewan:warn-file-path-input

Conversation

@UditDewan

@UditDewan UditDewan commented Jul 18, 2026

Copy link
Copy Markdown

Description

extract() treats string input as literal text. When a user passes a file path instead of the file's contents (an easy mistake), the path itself is sent to the model, which can echo the local directory structure and OS username into extraction results, or hallucinate content for the "document" — exactly the exposure reported in the linked issue.

This PR makes extract() emit a UserWarning when text_or_documents is a string that matches a path to an existing file, telling the user that the path (not the file contents) will be sent to the model and showing how to read the file first. The check is guarded to non-URL strings under 4096 chars, so normal document text never pays a filesystem stat. Behavior is otherwise unchanged: strings are still never read from disk, keeping the API free of any implicit file access. The text_or_documents docstring now documents this.

Fixes #249

Bug fix

How Has This Been Tested?

Added FilePathInputWarningTest to tests/init_test.py: a string matching an existing file path emits the warning; ordinary text does not.

$ python -m pytest tests/init_test.py
27 passed, 18 subtests passed

$ pylint --rcfile=tests/.pylintrc tests/init_test.py
Your code has been rated at 10.00/10

$ isort langextract tests && pyink langextract tests --config pyproject.toml
2 files left unchanged.

The full suite (pytest tests --ignore=tests/test_live_api.py) shows the same results as unmodified main in my environment, plus the two new tests.

Checklist:

  • I have read and acknowledged Google's Open Source
    Code of conduct.
  • I have read the
    Contributing
    page, and I either signed the Google
    Individual CLA
    or am covered by my company's
    Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked
    issue(s) and we have agreed upon the general approach.
  • I have made any needed documentation changes, or noted in the linked
    issue(s) that documentation elsewhere needs updating.
  • I have added tests, or I have ensured existing tests cover the changes
  • I have followed
    Google's Python Style Guide
    and ran pylint over the affected code.

LangExtract treats string input as literal text, so passing a file path
to extract() sends the path itself to the model. The model can then echo
the path -- including local directory structure and the OS username --
into extraction results, or hallucinate content for it (google#249).

Emit a UserWarning when text_or_documents matches an existing file so
the mistake is surfaced immediately, and document that strings are never
read from disk.

Fixes google#249
@github-actions github-actions Bot added the size/S Pull request with 50-150 lines changed label Jul 18, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Pull request with 50-150 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

potential exposure of local path and identity of the user

1 participant