Skip to content

Simplify connection config handling in Python SDK#849

Merged
jakubno merged 8 commits intomainfrom
improve-connection-config-handling-in-python-sdk-e2b-2838
Aug 10, 2025
Merged

Simplify connection config handling in Python SDK#849
jakubno merged 8 commits intomainfrom
improve-connection-config-handling-in-python-sdk-e2b-2838

Conversation

@jakubno
Copy link
Copy Markdown
Member

@jakubno jakubno commented Aug 6, 2025

Several improvements and refactors to the Python SDK's sandbox connection and configuration logic.

The main focus is on making header management more flexible, improving API parameter handling, and simplifying class structures for better maintainability and usability.

Also unifies doc strings

@jakubno jakubno self-assigned this Aug 6, 2025
@jakubno jakubno added Improvement Improvement for current functionality python Pull requests that update Python code labels Aug 6, 2025
@linear
Copy link
Copy Markdown

linear Bot commented Aug 6, 2025

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Aug 6, 2025

🦋 Changeset detected

Latest commit: 0c03293

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@e2b/python-sdk Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jakubno jakubno force-pushed the improve-connection-config-handling-in-python-sdk-e2b-2838 branch from 876a7c1 to a297a30 Compare August 6, 2025 14:44
@jakubno jakubno force-pushed the improve-connection-config-handling-in-python-sdk-e2b-2838 branch from a297a30 to d16de51 Compare August 6, 2025 14:46
@jakubno jakubno force-pushed the improve-connection-config-handling-in-python-sdk-e2b-2838 branch from d16de51 to 7570417 Compare August 6, 2025 14:52
@jakubno jakubno marked this pull request as ready for review August 6, 2025 14:54
Comment thread packages/python-sdk/e2b/sandbox/sandbox_api.py
Comment thread packages/python-sdk/e2b/connection_config.py Outdated
Comment thread packages/python-sdk/e2b/sandbox/main.py Outdated
@ValentaTomas
Copy link
Copy Markdown
Member

ValentaTomas commented Aug 6, 2025

One detail:

Here we can use SandboxApiBase._limits as everywhere else too (I forgot this one).

@jakubno jakubno force-pushed the improve-connection-config-handling-in-python-sdk-e2b-2838 branch from 1395b6e to 8ea3448 Compare August 7, 2025 08:28
@jakubno jakubno force-pushed the improve-connection-config-handling-in-python-sdk-e2b-2838 branch from 02d91db to 77997f1 Compare August 7, 2025 11:08
access_token: Optional[str] = None,
request_timeout: Optional[float] = None,
headers: Optional[Dict[str, str]] = None,
extra_sandbox_headers: Optional[Dict[str, str]] = None,
Copy link
Copy Markdown
Member

@mishushakov mishushakov Aug 7, 2025

Choose a reason for hiding this comment

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

I find it looks weird that you have both headers and extra_sandbox_headers
what about?

default_headers = {'User-Agent': 'xxx'}

def __init__(
    headers: Dict[str, str] = {}
):
    self.headers = {**default_headers, **headers}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mishushakov Are you asking why we need two sets of headers?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

@ValentaTomas ValentaTomas Aug 8, 2025

Choose a reason for hiding this comment

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

We don't want to be sending the "api" headers (like the supabase token) to the sandbox endpoints and we also don't want to send the "sandbox" headers (like the envd access token) to the API, so to prevent mixing them on the requests made from the methods on the Sandbox instance we need two separate sets.

It is possible that it can be handled more elegantly internally, but we are mostly making sure it behaves sanely to the outside now.

@mishushakov
Copy link
Copy Markdown
Member

@jakubno one question - do we still need this? maybe there's a way to better handle this

https://github.com/e2b-dev/E2B/blob/main/packages/python-sdk/e2b/sandbox_async/main.py#L507-L512

@mishushakov
Copy link
Copy Markdown
Member

Can we add docstring for options also?

@ValentaTomas
Copy link
Copy Markdown
Member

@jakubno one question - do we still need this? maybe there's a way to better handle this

https://github.com/e2b-dev/E2B/blob/main/packages/python-sdk/e2b/sandbox_async/main.py#L507-L512

@mishushakov I think this is already removed in this PR as it was both duplicated and there was a bug.

@jakubno
Copy link
Copy Markdown
Member Author

jakubno commented Aug 7, 2025

@jakubno one question - do we still need this? maybe there's a way to better handle this

https://github.com/e2b-dev/E2B/blob/main/packages/python-sdk/e2b/sandbox_async/main.py#L507-L512

It's basically removed, right?

@jakubno
Copy link
Copy Markdown
Member Author

jakubno commented Aug 7, 2025

@jakubno jakubno force-pushed the improve-connection-config-handling-in-python-sdk-e2b-2838 branch from 7be1034 to 4c0e609 Compare August 7, 2025 20:09
@ValentaTomas
Copy link
Copy Markdown
Member

Can we add docstring for options also?

You mean for the Unpacked option? I'm not sure it will be visible as a separate field.

@jakubno
Copy link
Copy Markdown
Member Author

jakubno commented Aug 7, 2025

Can we add docstring for options also?

You mean for the Unpacked option? I'm not sure it will be visible as a separate field.

I could only add something like

:param opts Additional API request options

But there's a risk it will be obsolete

cursor[bot]

This comment was marked as outdated.

with ApiClient(
config,
limits=SandboxApiBase._limits,
limits=cls._limits,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: API Client Limits Increased After Class Change

The class inheritance change from SandboxApiBase to SandboxBase (and subsequent use of cls._limits) unintentionally increased HTTP connection limits for API client operations in both sync and async sandbox API methods. The limits changed from max_keepalive_connections=10, max_connections=20, keepalive_expiry=20 to max_keepalive_connections=40, max_connections=40, keepalive_expiry=300. This results in significantly larger connection pool sizes and longer timeouts, potentially increasing resource usage.

Additional Locations (1)
Fix in Cursor Fix in Web

@jakubno jakubno merged commit da7c60d into main Aug 10, 2025
8 checks passed
@jakubno jakubno deleted the improve-connection-config-handling-in-python-sdk-e2b-2838 branch August 10, 2025 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Improvement Improvement for current functionality python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants