feat(py): Allow deferred chat client initialization#207
Open
feat(py): Allow deferred chat client initialization#207
Conversation
When using Posit Connect managed OAuth credentials, chat client connections need access to HTTP headers in the Shiny session object, requiring creation inside the server function. Changes: - Defer client initialization when data_source=None and client=None - Add chat_client property getter/setter for setting client after init - Add client parameter to server() method for deferred pattern - Add _require_client() method for runtime checks - Update methods (client, console, generate_greeting) to require client Closes #205 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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
clientto be deferred whendata_source=Noneand no explicit client is providedchat_clientproperty getter/setter for setting client after initializationclientparameter toserver()method for deferred pattern_require_client()method for runtime checks on methods that need the clientMotivation
When using Posit Connect managed OAuth credentials, all data/model connections must be created in the Shiny
serverfunction, as connections using managed credentials need access to the HTTP headers in the Shinysessionobject. Previously, when creating theQueryChatclass without an explicit client, it would attempt to connect to OpenAI, resulting in an exception whenOPENAI_API_KEYwasn't set.Now, when
data_source=None(the deferred pattern), the client is also deferred by default:Closes #205
Test plan
chat_clientproperty setter🤖 Generated with Claude Code