Skip to content

environments.py:get_variables() in v0.4.1b1 seems broken #228

@sondrehaug

Description

@sondrehaug

Scenario & Reproduction Steps

I am trying out the Environment beta functionality implemented in #210 by @edif2008

Pull repo and check out to v0.4.1b1. Add the file below, which is only testing the environments.get_variables part from onepassword-sdk-python/example/desktop_app.py:

# onepassword-sdk-python/example/environment_read_via_desktop_app_auth.py
from onepassword import *
import asyncio


async def main() -> None:
    client = await Client.authenticate(
        auth=DesktopAuth(account_name="My Company"),
        integration_name="1Password Environment Smoke Test",
        integration_version="v1.0.0",
    )

    environment = await client.environments.get_variables("<my-env-id>")

    for variable in environment.variables:
        print(f"{variable.name}: {variable.value} (masked: {variable.masked})")


if __name__ == "__main__":
    asyncio.run(main())

And ran:

python -m venv .venv
source .venv/bin/activate
pip install -e .
python example/environment_read_via_desktop_app_auth.py 

Actual Behavior

Desktop auth window popped up, I authenticated and it ran into this pydantic error:

$ python example/environment_read_via_desktop_app_auth.py 
Traceback (most recent call last):
  File "/home/user/repos/onepassword-sdk-python/example/environment_read_via_desktop_app_auth.py", line 19, in <module>
    asyncio.run(main())
    ~~~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/usr/lib/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/usr/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/home/user/repos/onepassword-sdk-python/example/environment_read_via_desktop_app_auth.py", line 12, in main
    environment = await client.environments.get_variables("<masked>")
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/repos/onepassword-sdk-python/src/onepassword/environments.py", line 32, in get_variables
    response = TypeAdapter(GetVariablesResponse).validate_json(response)
  File "/home/user/repos/onepassword-sdk-python/.venv/lib/python3.13/site-packages/pydantic/type_adapter.py", line 492, in validate_json
    return self.validator.validate_json(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        data,
        ^^^^^
    ...<5 lines>...
        by_name=by_name,
        ^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 2 validation errors for GetVariablesResponse
variables.0.name
  Field required [type=missing, input_value={'key': 'ENV_A', 'value': 'a', 'masked': True}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.13/v/missing
variables.1.name
  Field required [type=missing, input_value={'key': 'ENV_B', 'value': 'b', 'masked': True}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.13/v/missing

The stack trace shows it's able to get the environment variables (ENV_A and ENV_B) and their values from my 1Password Environment, but pydantic validation fails.

Expected Behavior

Environment variables printed

SDK version

No response

Additional information

Root cause could be that the EnvironmentsGetVariables endpoint returns key in stead of name in the response here:

    async def get_variables(self, environment_id: str) -> GetVariablesResponse:
        """
        Get environment variables belonging to an Environment.
        """
        response = await self.inner_client.invoke(
            {
                "invocation": {
                    "clientId": self.inner_client.client_id,
                    "parameters": {
                        "name": "EnvironmentsGetVariables",
                        "parameters": {"environment_id": environment_id},
                    },
                }
            }
        )

The fix could be to modify onepassword-sdk-python/src/onepassword/types.py:EnvironmentVariable from name to key:

class EnvironmentVariable(BaseModel):
    """
    Represents an environment variable (name:value pair) and its masked state
    """

    key: str
    """
    An environment variable's name
    """
    value: str
    """
    An environment variable's value
    """
    masked: bool
    """
    An environment variable's masked state
    """

Which worked, and resulted in this output:

$ python example/environment_read_via_desktop_app_auth.py 
ENV_A: a (masked: True)
ENV_B: b (masked: True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions