Skip to content

perf(client): add session pooling for connection reuse TAV-4105#151

Closed
tinosattavily wants to merge 1 commit into
tavily-ai:masterfrom
tinosattavily:feat/TAV-4105-session-pooling
Closed

perf(client): add session pooling for connection reuse TAV-4105#151
tinosattavily wants to merge 1 commit into
tavily-ai:masterfrom
tinosattavily:feat/TAV-4105-session-pooling

Conversation

@tinosattavily
Copy link
Copy Markdown
Contributor

@tinosattavily tinosattavily commented Jan 26, 2026

Summary

  • Add requests.Session() to TavilyClient for HTTP connection pooling
  • Reuse TCP connections and TLS handshakes across API calls
  • Reduces per-request overhead (~100-300ms) for repeated calls

Test plan

  • Verify existing tests pass
  • Test multiple sequential API calls show connection reuse
  • Confirm no breaking changes to public API

Fixes TAV-4105


Note

Improves HTTP performance by introducing a shared requests.Session in tavily/tavily.py and using it across all client requests for connection reuse.

  • Initializes self.session with default headers and optional proxies; removes per-call headers/proxies arguments
  • Replaces requests.post/get with self.session.post/get for search, extract, crawl, map, research (including streaming), and get_research

No public API changes; error handling and response parsing remain the same.

Written by Cursor Bugbot for commit 31d0694. This will update automatically on new commits. Configure here.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

Comment thread tavily/tavily.py
self.session = requests.Session()
self.session.headers.update(self.headers)
if self.proxies:
self.session.proxies.update(self.proxies)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shared session used concurrently is not thread-safe

Medium Severity

The new requests.Session object is shared across threads in get_company_info, which uses ThreadPoolExecutor to make parallel API calls. requests.Session is not documented as thread-safe, and the previous implementation using independent requests.post() calls was explicitly safe for concurrent use. This change introduces potential race conditions when multiple threads access the session simultaneously.

Additional Locations (1)

Fix in Cursor Fix in Web

Comment thread tavily/tavily.py
self.session = requests.Session()
self.session.headers.update(self.headers)
if self.proxies:
self.session.proxies.update(self.proxies)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Session pooling introduced without cleanup mechanism

Low Severity

The requests.Session is created and holds connection pools, but there's no close() method, __del__ destructor, or context manager support (__enter__/__exit__) to release these resources. Before this change, each request created and released its own connection. Now resources accumulate until garbage collection. Users creating multiple TavilyClient instances (e.g., in loops or long-running applications) could hit file descriptor limits or experience memory growth. The async client handles this correctly by creating/closing clients per-request with context managers.

Fix in Cursor Fix in Web

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.

1 participant