Add redirect_stdout_to_stderr() utility for native libraries - #1635
Open
fly1d wants to merge 1 commit into
Open
Conversation
Some native model backends (llama.cpp, gpt4all) write log lines directly to file descriptor 1, bypassing sys.stdout and contextlib.redirect_stdout. Expose a reusable context manager that redirects fd 1 to stderr so plugins can keep that noise off stdout. Assisted-by: OpenAI Codex (GPT-5)
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.
Summary
Fixes #196.
Some native model backends write log messages straight to file descriptor 1, bypassing both
sys.stdoutandcontextlib.redirect_stdout. This is visible with thellm-llama-cppandllm-gpt4allplugins, where llama.cpp/gpt4all emit lines such asllama_new_context_with_model: ...directly to stdout.This adds a small reusable
redirect_stdout_to_stderr()context manager tollm.utilsthat redirects OS-level stdout (fd 1) to stderr for the duration of the block. Plugins can wrap their native init and generate calls with it, so that noise stays on stderr and stdout keeps only the model output.Verification
pytest tests/test_utils.py -q: 86 passedruff check llm/utils.py tests/test_utils.py: passedblack --check llm/utils.py tests/test_utils.py: passedThe new test uses
os.pipe()to observe fd-level writes directly, so it verifies the redirect works for writes that bypass Python'ssys.stdout.Prepared with OpenAI Codex (GPT-5) assistance.