{"record":{"id":"01462d171a008cce","repo":"github/copilot-sdk","slug":"set-environment-variables-via-either-the-client-le-01462d","errorCode":null,"errorMessage":"Set environment variables via either the client-level env argument or ChildProcessRuntimeConnection.env, not both. Prefer the connection-level env for child-process transports.","messagePattern":"Set environment variables via either the client-level env argument or ChildProcessRuntimeConnection\\.env, not both\\. Prefer the connection-level env for child-process transports\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/copilot/client.py","lineNumber":1519,"sourceCode":"                \"telemetry cannot be honored in-process. Configure telemetry via the \"\n                \"host process environment, or use a child-process transport.\"\n            )\n        if options.working_directory is not None:\n            raise ValueError(\n                \"working_directory is not supported with RuntimeConnection.for_inprocess(): \"\n                \"the native runtime shares the host process working directory, so a \"\n                \"per-client working directory cannot be honored in-process. Use a \"\n                \"child-process \"\n                \"transport, or set the process working directory before creating the client.\"\n            )\n        return\n\n    if (\n        isinstance(connection, ChildProcessRuntimeConnection)\n        and connection.env is not None\n        and options.env is not None\n    ):\n        raise ValueError(\n            \"Set environment variables via either the client-level env argument or \"\n            \"ChildProcessRuntimeConnection.env, not both. Prefer the connection-level \"\n            \"env for child-process transports.\"\n        )\n\n\nclass CopilotClient:\n    \"\"\"\n    Main client for interacting with the Copilot CLI.\n\n    The CopilotClient manages the connection to the Copilot CLI server and provides\n    methods to create and manage conversation sessions. It can either spawn a CLI\n    server process or connect to an existing server.\n\n    The client supports both stdio (default) and TCP transport modes for\n    communication with the CLI server.\n\n    Example:","sourceCodeStart":1501,"sourceCodeEnd":1537,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/python/copilot/client.py#L1501-L1537","documentation":"Environment variables can be provided either via the client-level env option or via ChildProcessRuntimeConnection.env; supplying both is ambiguous and rejected with ValueError. The library asks callers to prefer the connection-level env for child-process transports.","triggerScenarios":"Creating CopilotClient with a ChildProcessRuntimeConnection whose env is set AND passing a non-None env in _CopilotClientOptions.","commonSituations":"A wrapper that always sets client-level env while the connection object was built with env; refactoring code that moved env from options to the connection but left the old value in place.","solutions":["Remove the client-level env option and keep env on ChildProcessRuntimeConnection.env (preferred)","Or remove connection.env and keep the client-level env argument"],"exampleFix":"// before\nconn = RuntimeConnection.for_stdio(env={\"FOO\": \"1\"})\nclient = CopilotClient(connection=conn, env={\"BAR\": \"2\"})\n// after\nconn = RuntimeConnection.for_stdio(env={\"FOO\": \"1\", \"BAR\": \"2\"})\nclient = CopilotClient(connection=conn)","handlingStrategy":"validation","validationCode":"if isinstance(conn, ChildProcessRuntimeConnection) and conn.env is not None and opts.env is not None:\n    raise ValueError(\"set env on connection or options, not both\")","typeGuard":"def env_config_is_unambiguous(conn, opts) -> bool:\n    return not (isinstance(conn, ChildProcessRuntimeConnection) and conn.env is not None and opts.env is not None)","tryCatchPattern":"try:\n    client = CopilotClient(connection=conn, env=env)\nexcept ValueError as e:\n    if \"not both\" in str(e):\n        client = CopilotClient(connection=conn)  # connection.env wins","preventionTips":["Adopt one convention: always set env on the connection object for child-process transports","Wrap client construction in a helper that merges env into the connection","Add a unit test asserting env is only ever set in one place"],"tags":["python","configuration","child-process","environment"],"backgroundTag":"conflicting-config-options","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}