Skip to content

feat(py): Allow deferred chat client initialization#207

Open
cpsievert wants to merge 1 commit intomainfrom
cpsievert/issue-205-deferred-client
Open

feat(py): Allow deferred chat client initialization#207
cpsievert wants to merge 1 commit intomainfrom
cpsievert/issue-205-deferred-client

Conversation

@cpsievert
Copy link
Contributor

Summary

  • Allow client to be deferred when data_source=None and no explicit client is provided
  • Add chat_client property getter/setter for setting client after initialization
  • Add client parameter to server() method for deferred pattern
  • Add _require_client() method for runtime checks on methods that need the client

Motivation

When using Posit Connect managed OAuth credentials, all data/model connections must be created in the Shiny server function, as connections using managed credentials need access to the HTTP headers in the Shiny session object. Previously, when creating the QueryChat class without an explicit client, it would attempt to connect to OpenAI, resulting in an exception when OPENAI_API_KEY wasn't set.

Now, when data_source=None (the deferred pattern), the client is also deferred by default:

# No error - client is deferred when data_source is None
qc = querychat.QueryChat(
    None,
    table_name="mtcars",
    greeting=greeting,
    data_description=data_desc
)

def server(input, output, session):
    # Set client in server where session credentials are available
    qc_vals = qc.server(
        client=ChatAnthropic(),
        data_source=mtcars_table
    )

Closes #205

Test plan

  • Added unit tests for deferred client initialization pattern
  • Added tests for chat_client property setter
  • Added tests that methods properly require client to be set
  • Added integration tests for full deferred workflow
  • Updated existing deferred data source test
  • All 301 tests pass

🤖 Generated with Claude Code

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>
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.

(py) Allow QueryChat class to be created with an empty chat client

1 participant