Warn when string input matches an existing file path - #489
Open
UditDewan wants to merge 4 commits into
Open
Conversation
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
|
Your branch is 1 commits behind git fetch origin main
git merge origin/main
git pushNote: Enable "Allow edits by maintainers" to allow automatic updates. |
|
Your branch is 1 commits behind git fetch origin main
git merge origin/main
git pushNote: Enable "Allow edits by maintainers" to allow automatic updates. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aUserWarningwhentext_or_documentsis 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. Thetext_or_documentsdocstring now documents this.Fixes #249
Bug fix
How Has This Been Tested?
Added
FilePathInputWarningTesttotests/init_test.py: a string matching an existing file path emits the warning; ordinary text does not.The full suite (
pytest tests --ignore=tests/test_live_api.py) shows the same results as unmodifiedmainin my environment, plus the two new tests.Checklist:
Code of conduct.
Contributing
page, and I either signed the Google
Individual CLA
or am covered by my company's
Corporate CLA.
issue(s) and we have agreed upon the general approach.
issue(s) that documentation elsewhere needs updating.
Google's Python Style Guide
and ran
pylintover the affected code.