Repository navigation
docs: Environment Builder client uses .sync() handle (from #1129) #1166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a25e9f5
95cd5d3
dee25b7
ac3d3df
6a7087d
0dfebdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ Already familiar with OpenEnv? Here's the 8-step process at a glance: | |
| | 5 | `uv run --project . server` | Start local dev server for testing | | ||
| | 6 | `openenv validate --level static --skip-build` | Validate the declared manifest contract | | ||
| | 7 | `openenv push` | Deploy to Hugging Face Hub | | ||
| | 8 | Share the URL! | Others use via `MyEnv.from_hub("you/my-env")` | | ||
| | 8 | Share it! | Others use via `MyEnv.from_env("you/my-env").sync()` | | ||
|
|
||
| ### CLI Quick Reference | ||
|
|
||
|
|
@@ -410,12 +410,12 @@ Here is a simple example of using your environment: | |
| ```python | ||
| from envs.my_env import MyAction, MyEnv | ||
|
|
||
| # Create environment from Docker image | ||
| client = MyEnv.from_docker_image("my-env:latest") | ||
| # Or, connect to the remote space on Hugging Face | ||
| client = MyEnv.from_hub("my-org/my-env") | ||
| # Or, connect to the local server | ||
| client = MyEnv(base_url="http://localhost:8000") | ||
| # Create environment from Docker image (starts a container) | ||
| client = MyEnv.from_docker_image("my-env:latest").sync() | ||
| # Or, run the image of a Hugging Face Space locally | ||
| client = MyEnv.from_env("my-org/my-env").sync() | ||
| # Or, connect to an already running server | ||
| client = MyEnv(base_url="http://localhost:8000").sync() | ||
|
|
||
| # Use context manager for automatic cleanup (recommended) | ||
| with client: | ||
|
|
@@ -435,13 +435,40 @@ with client: | |
|
|
||
| # Or manually manage the connection | ||
| try: | ||
| client = MyEnv(base_url="http://localhost:8000") | ||
| client = MyEnv(base_url="http://localhost:8000").sync() | ||
| result = client.reset() | ||
| result = client.step(MyAction(command="test", parameters={})) | ||
| finally: | ||
| client.close() | ||
| ``` | ||
|
|
||
| `from_docker_image()` and `from_env()` do not return a connected client. They | ||
| return a lazy bootstrap handle, and nothing starts until you resolve it: chain | ||
| `.sync()` for a synchronous client (as above), or `await` the handle from async | ||
| code. Using the handle directly in a `with` block raises | ||
| `TypeError: '_BootstrapResult' object does not support the context manager protocol`. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified end-to-end: on Python 3.12, |
||
|
|
||
| The equivalent async usage is: | ||
|
|
||
| ```python | ||
| import asyncio | ||
|
|
||
| from envs.my_env import MyAction, MyEnv | ||
|
|
||
|
|
||
| async def main(): | ||
| client = await MyEnv.from_docker_image("my-env:latest") | ||
| async with client: | ||
| result = await client.reset() | ||
| result = await client.step(MyAction(command="test", parameters={})) | ||
| state = await client.state() | ||
|
|
||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| See [Async vs Sync Usage](../guides/async-sync) for when to prefer each style. | ||
|
|
||
| ## Nice work! You've now built and used your own OpenEnv environment. | ||
|
|
||
| Your next steps are to: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good fix.
EnvClienthas nofrom_hub(it only exists onAutoEnv/AutoAction), so the previousMyEnv.from_hub(...)example would have raisedAttributeError.from_env(...)(env_client.py:814) with the defaultuse_docker=Truepullsregistry.hf.space/…and runs it locally viaLocalDockerProvider, which matches the "run the image … locally" comment.Follow-up (out of scope for this PR):
docs/source/tutorials/rl-training-2048.md:147and:469still use the same brokenOpenSpielEnv.from_hub(...)pattern — worth migrating tofrom_env(...)in a later pass.