Skip to content

Fix/open utf8 encodingfix: specify utf-8 encoding for text file reads#4350

Open
LuoisaWu wants to merge 2 commits into
traceloop:mainfrom
LuoisaWu:fix/open-utf8-encoding
Open

Fix/open utf8 encodingfix: specify utf-8 encoding for text file reads#4350
LuoisaWu wants to merge 2 commits into
traceloop:mainfrom
LuoisaWu:fix/open-utf8-encoding

Conversation

@LuoisaWu

@LuoisaWu LuoisaWu commented Jul 4, 2026

Copy link
Copy Markdown

Summary

Fixes #4286.

This PR adds explicit encoding="utf-8" to text-mode open() calls that read JSON / Swagger files.

This avoids relying on the platform default encoding, which can fail on systems where the default encoding is not UTF-8, such as Windows environments using GBK.

Changes

  • Add encoding="utf-8" when loading Qdrant method metadata JSON files.
  • Add encoding="utf-8" when loading the Swagger spec in the evaluator model codegen script.

Verification

  • Ran:
python -m compileall packages/opentelemetry-instrumentation-qdrant/opentelemetry/instrumentation/qdrant/__init__.py scripts/codegen/generate_evaluator_models.py

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **New Features**
  * Added a new sample workflow demonstrating how to use an Aleph Alpha client with tracing enabled.
  * Expanded the sample app setup to include the required tracing integration and supporting dependency.

* **Bug Fixes**
  * Improved file-loading reliability by explicitly using UTF-8 encoding when reading configuration and API schema files.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@CLAassistant

CLAassistant commented Jul 4, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds explicit UTF-8 encoding to file open() calls in the Qdrant instrumentation module and the evaluator models codegen script, and introduces a new Aleph Alpha sample application script along with its dependency wiring in the sample-app package.

Changes

UTF-8 Encoding Fixes

Layer / File(s) Summary
Qdrant client method file loading
packages/opentelemetry-instrumentation-qdrant/opentelemetry/instrumentation/qdrant/__init__.py
Adds encoding="utf-8" to open() calls loading sync/async Qdrant client method JSON files, with clarifying comments.
Evaluator models codegen file loading
scripts/codegen/generate_evaluator_models.py
Adds encoding="utf-8" to the open() call reading the Swagger/OpenAPI JSON file.

Aleph Alpha Sample App

Layer / File(s) Summary
Dependency wiring
packages/sample-app/pyproject.toml
Adds aleph-alpha-client and opentelemetry-instrumentation-alephalpha dependencies and an editable local source mapping.
Example script
packages/sample-app/sample_app/alephalpha_example.py
Adds a new script that initializes Traceloop, creates an Aleph Alpha client, defines a traced complete_prompt task and alephalpha_completion_demo workflow, and a __main__ entrypoint.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Related issues: #4286 (fix: open() calls missing encoding='utf-8')

Suggested labels: bug, enhancement, dependencies

Suggested reviewers: nirga


🐰 A rabbit hopped through code so wide,
Adding utf-8 with pride,
Then found a sample script anew,
Aleph Alpha joins the crew,
Hop hop, changes verified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The Aleph Alpha sample app dependency/example additions are unrelated to #4286's encoding-only fix. Move the Aleph Alpha sample app changes into a separate PR or remove them from this encoding fix.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly references the utf-8 encoding fix for open() calls, matching the main change.
Linked Issues check ✅ Passed The PR implements the requested utf-8 encoding on the Qdrant JSON reads and the Swagger codegen read.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
scripts/codegen/generate_evaluator_models.py (1)

27-28: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove leftover commented-out code / trailing whitespace.

Line 27 keeps the old open() call as a comment and line 28 has trailing whitespace.

♻️ Proposed cleanup
-    # with open(swagger_path) as f:  the same as above, we need to specify encoding
-    with open(swagger_path, encoding="utf-8") as f:  
+    with open(swagger_path, encoding="utf-8") as f:
         data = json.load(f)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/codegen/generate_evaluator_models.py` around lines 27 - 28, Remove
the leftover commented-out `open()` line and trim the trailing whitespace in the
`generate_evaluator_models.py` block; keep the actual `with open(swagger_path,
encoding="utf-8") as f:` call and clean up the surrounding formatting so the
`open` usage is only represented once and the line is whitespace-free.
packages/opentelemetry-instrumentation-qdrant/opentelemetry/instrumentation/qdrant/__init__.py (1)

26-32: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove leftover commented-out code.

Lines 26 and 31 keep the old open() calls as comments; line 32 also has trailing whitespace. Consider dropping the dead comments and trailing whitespace for cleanliness.

♻️ Proposed cleanup
 p = Path(__file__).with_name("qdrant_client_methods.json")
-# with open(p, "r") as f:  --first version of the file was not utf-8 encoded, so we need to specify encoding
 with open(p, "r", encoding="utf-8") as f:
     QDRANT_CLIENT_METHODS = json.loads(f.read())

 p = Path(__file__).with_name("async_qdrant_client_methods.json")
-# with open(p, "r") as f:   the same as above, we need to specify encoding
-with open(p, "r", encoding="utf-8") as f:   
+with open(p, "r", encoding="utf-8") as f:
     ASYNC_QDRANT_CLIENT_METHODS = json.loads(f.read())
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/opentelemetry-instrumentation-qdrant/opentelemetry/instrumentation/qdrant/__init__.py`
around lines 26 - 32, Remove the leftover commented-out open() lines in the
qdrant instrumentation module and clean up the trailing whitespace. Update the
import-time JSON loading code in __init__.py around the QDRANT_CLIENT_METHODS
and async_qdrant_client_methods.json reads so only the active with open(...,
encoding="utf-8") calls remain, with no dead comments or extra spaces.
packages/sample-app/sample_app/alephalpha_example.py (1)

8-10: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add a docstring documenting required env vars.

Other sample scripts (e.g. litellm_completion.py) document required environment variables and TRACELOOP_API_KEY needs upfront in a module docstring. This script silently falls back between ALEPH_ALPHA_API_KEY/AA_TOKEN and will only fail at client.complete() call time with an unclear error if neither is set.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/sample-app/sample_app/alephalpha_example.py` around lines 8 - 10,
Add a module docstring at the top of alephalpha_example.py that documents the
required environment variables, matching the style used in other sample scripts
like litellm_completion.py. Mention that TRACELOOP_API_KEY is required upfront
and that the Aleph Alpha client expects either ALEPH_ALPHA_API_KEY or AA_TOKEN,
so users know what must be set before running the sample. Keep the guidance near
Traceloop.init and Client initialization so the required setup is clear
immediately.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In
`@packages/opentelemetry-instrumentation-qdrant/opentelemetry/instrumentation/qdrant/__init__.py`:
- Around line 26-32: Remove the leftover commented-out open() lines in the
qdrant instrumentation module and clean up the trailing whitespace. Update the
import-time JSON loading code in __init__.py around the QDRANT_CLIENT_METHODS
and async_qdrant_client_methods.json reads so only the active with open(...,
encoding="utf-8") calls remain, with no dead comments or extra spaces.

In `@packages/sample-app/sample_app/alephalpha_example.py`:
- Around line 8-10: Add a module docstring at the top of alephalpha_example.py
that documents the required environment variables, matching the style used in
other sample scripts like litellm_completion.py. Mention that TRACELOOP_API_KEY
is required upfront and that the Aleph Alpha client expects either
ALEPH_ALPHA_API_KEY or AA_TOKEN, so users know what must be set before running
the sample. Keep the guidance near Traceloop.init and Client initialization so
the required setup is clear immediately.

In `@scripts/codegen/generate_evaluator_models.py`:
- Around line 27-28: Remove the leftover commented-out `open()` line and trim
the trailing whitespace in the `generate_evaluator_models.py` block; keep the
actual `with open(swagger_path, encoding="utf-8") as f:` call and clean up the
surrounding formatting so the `open` usage is only represented once and the line
is whitespace-free.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 85f55567-34b5-4ea6-b274-624c289e0ae7

📥 Commits

Reviewing files that changed from the base of the PR and between 93429cf and 5167ad6.

📒 Files selected for processing (4)
  • packages/opentelemetry-instrumentation-qdrant/opentelemetry/instrumentation/qdrant/__init__.py
  • packages/sample-app/pyproject.toml
  • packages/sample-app/sample_app/alephalpha_example.py
  • scripts/codegen/generate_evaluator_models.py

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.

fix: open() calls missing encoding='utf-8' may crash on non-UTF-8 systems

2 participants