Skip to content

Refactor API client initialization and fix Pydantic validation#26

Merged
tmaier-kettering merged 1 commit into
mainfrom
claude/project-review-improvements-nryvsh
Jul 7, 2026
Merged

Refactor API client initialization and fix Pydantic validation#26
tmaier-kettering merged 1 commit into
mainfrom
claude/project-review-improvements-nryvsh

Conversation

@tmaier-kettering

Copy link
Copy Markdown
Owner

Summary

This PR refactors the OpenAI API client initialization across live processing modules to use a centralized get_client() function, improves error handling with user-facing messages, and updates Pydantic field validation to use the correct parameter names.

Key Changes

  • Centralized API client initialization: Replaced duplicated try-except blocks in keyword_extraction_live.py, multi_label_live.py, and single_label_live.py with a call to get_client() from batch_processing.batch_method, reducing code duplication and improving maintainability.

  • Improved error handling: Added user-facing error dialogs using messagebox.showerror() when API key initialization fails, providing better feedback to users instead of silent failures.

  • Pydantic validation fix: Updated all Field() definitions from deprecated min_items parameter to min_length across:

    • keyword_extraction_live.py
    • multi_label_live.py
    • single_label_live.py
    • batch_processing/batch_creation.py
  • Removed unused imports: Eliminated direct imports of OpenAI and secrets_store from live processing modules, as these are now handled by the centralized get_client() function.

  • Cross-platform icon handling: Added try-except block in ui/main_window.py to gracefully handle .ico file loading on non-Windows platforms where iconbitmap() raises TclError.

  • Code cleanup: Removed unused helper functions _current_header_labels() and _selected_header_label() from file_handling/data_import.py, and removed unused tkinter import from batch_processing/batch_creation.py.

Implementation Details

The refactoring consolidates API client initialization into a single source of truth, making it easier to manage API key handling and error states across the application. The Pydantic updates ensure compatibility with current versions of the library that have deprecated the min_items parameter in favor of min_length.

https://claude.ai/code/session_012vWd3zZNHnkBSNeCGVjYWb

- Live Methods (single/multi-label classification, keyword extraction)
  cached the OpenAI client at import time, so entering an API key in
  Settings never took effect for them (only Batch Methods refreshed
  correctly). They now fetch a fresh client via batch_method.get_client()
  at call time and show a clear error if no key is configured.
- Main window crashed on startup on Linux/macOS because .ico icons only
  work with iconbitmap on Windows; now the failure is caught gracefully.
- Removed dead duplicate _selected_header_label/_current_header_labels
  definitions in data_import.py and an unused tkinter import in
  batch_creation.py.
- Replaced Pydantic's deprecated min_items with min_length to remove
  deprecation warnings ahead of Pydantic v3.
@tmaier-kettering tmaier-kettering merged commit ce1ee41 into main Jul 7, 2026
6 checks passed
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.

2 participants