Refactor API client initialization and fix Pydantic validation#26
Merged
Merged
Conversation
- 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.
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
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, andsingle_label_live.pywith a call toget_client()frombatch_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 deprecatedmin_itemsparameter tomin_lengthacross:keyword_extraction_live.pymulti_label_live.pysingle_label_live.pybatch_processing/batch_creation.pyRemoved unused imports: Eliminated direct imports of
OpenAIandsecrets_storefrom live processing modules, as these are now handled by the centralizedget_client()function.Cross-platform icon handling: Added try-except block in
ui/main_window.pyto gracefully handle.icofile loading on non-Windows platforms whereiconbitmap()raisesTclError.Code cleanup: Removed unused helper functions
_current_header_labels()and_selected_header_label()fromfile_handling/data_import.py, and removed unusedtkinterimport frombatch_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_itemsparameter in favor ofmin_length.https://claude.ai/code/session_012vWd3zZNHnkBSNeCGVjYWb