Skip to content

fix(agents): use venv for pip fallback in azd ai agent run#8787

Merged
trangevi merged 6 commits into
mainfrom
trangevi/fix-pip-venv-install
Jun 26, 2026
Merged

fix(agents): use venv for pip fallback in azd ai agent run#8787
trangevi merged 6 commits into
mainfrom
trangevi/fix-pip-venv-install

Conversation

@trangevi

Copy link
Copy Markdown
Member

Why

When uv is not installed, azd ai agent run falls back to pip for Python dependency installation. However, the pip fallback path was installing packages into the global Python environment instead of a .venv, unlike the uv path which always creates and uses a virtual environment. This inconsistency causes global package pollution and surprising behavior for users without uv.

What changed

Updated installPythonDepsPip to mirror the uv path behavior:

  • Creates a .venv using python -m venv if one doesn't already exist (uses stdlib, no extra tools needed)
  • Uses the venv's pip binary (<venv>/bin/pip or <venv>/Scripts/pip.exe) instead of system pip for all installs
  • Adds pyproject.toml support via pip install -e . -- the uv path already supported this but the pip fallback did not

Added helper functions venvPip() (returns the OS-appropriate path to the venv pip) and findSystemPython() (locates python3 or python on PATH for initial venv creation).

Testing

  • Existing TestResolveVenvCommand tests continue to pass
  • Added TestVenvPip and TestFindSystemPython unit tests
  • Build and lint pass cleanly

Fixes: #8280

When uv is not available, installPythonDepsPip now creates a .venv using
python -m venv (matching the uv path behavior) and installs packages into
it using the venv's pip binary. Also adds pyproject.toml support via
pip install -e . for the pip fallback path.

Fixes #8280

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 23, 2026 21:52
@github-actions

Copy link
Copy Markdown

📋 Prioritization Note

Thanks for the contribution! The linked issue isn't in the current milestone yet.
Thank you for logging this issue; our team is reviewing it. If you need urgent prioritization, tag @RickWinter and @kristenwomack to let us know.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR makes the azd ai agent run Python dependency installation behavior consistent when uv is missing by ensuring the pip fallback installs into a project-local .venv (instead of the global Python environment), and adds editable installs for pyproject.toml projects.

Changes:

  • Create .venv via python -m venv in the pip fallback path when missing, and run installs using the venv’s pip.
  • Add pyproject.toml support to the pip fallback via pip install -e ..
  • Introduce helper functions for locating venv pip and a system Python, plus unit tests for those helpers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
cli/azd/extensions/azure.ai.agents/internal/cmd/run.go Updates pip fallback to create/use .venv, adds pyproject.toml editable install support, and introduces venvPip/findSystemPython helpers.
cli/azd/extensions/azure.ai.agents/internal/cmd/run_test.go Adds unit tests for venvPip and findSystemPython.

Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/run.go
Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/run.go Outdated
Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/run_test.go
@github-actions github-actions Bot added the ext-agents azure.ai.agents extension label Jun 23, 2026
- Handle non-IsNotExist stat errors and validate .venv is a directory
- Change findSystemPython to return (string, error) with actionable message
- Add 'py' launcher fallback on Windows
- Rewrite TestFindSystemPython with controlled PATH for deterministic testing

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@jongio jongio left a comment

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.

Clean, well-scoped fix that brings the pip fallback in line with the uv path. The switch-based stat handling is solid, helper functions follow existing patterns (venvPython, venvBinDir), and tests use controlled PATH for determinism. Nice work addressing the earlier Copilot feedback in the follow-up commit.

One nit below about the version string in findSystemPython.

Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/run.go
Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/run.go

@jongio jongio left a comment

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.

The pip fallback venv reuse has a gap when .venv was previously created by uv venv, which doesn't include pip by default. If uv later becomes unavailable, the pip path skips venv creation (.venv exists) but venvPip() returns a path to a missing binary. This produces a confusing exec error rather than actionable guidance.

Consider checking fileExists(pipPath) before invoking it and running python -m ensurepip --upgrade as a bootstrap, or surfacing a clear error pointing the user to delete .venv and retry.

Not blocking for this PR since the core fix (venv isolation for pip fallback) is correct and well-tested. The uv-to-pip transition is an edge case that can be addressed in a follow-up.

trangevi and others added 2 commits June 24, 2026 16:30
…3.13+

- Bootstrap pip via ensurepip when venv exists but pip binary is missing
  (handles venvs created by uv which omits pip by default)
- Add checkPythonVersion to fail if system Python < 3.13, matching the
  uv path's --python '>=3.13' constraint
- Add TestCheckPythonVersion

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@jongio jongio left a comment

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.

Incremental review of 2 new commits (2225262, 7442f9c) since my last review.

Both prior findings are addressed:

  • Version enforcement: checkPythonVersion now enforces >=3.13 parity with the uv path's --python ">=3.13" constraint. Parsing logic handles standard CPython and non-standard (e.g. PyPy) version strings correctly.
  • uv-to-pip transition: the ensurepip bootstrap detects a pip-less venv (from uv venv) and runs python -m ensurepip --upgrade before dependency installation. Placement after the venv existence check and before pip install is correct.

No new issues in the incremental diff. The test for checkPythonVersion exercises the real system Python, which validates format parsing. It doesn't exercise the "too old" rejection path deterministically (would need a mock), but the version comparison logic is straightforward.

@jongio jongio left a comment

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.

Incremental review of 996234b (cspell config relocation).

Moving ensurepip from the core cli/azd/.vscode/cspell.yaml to the extension's own cspell.yaml is correct per the extension-specific config convention.

All findings from prior reviews are addressed. No new issues.

One blocker before merge: conflict in cli/azd/extensions/azure.ai.agents/cspell.yaml where main added recordproxy to the same section. Trivial resolution (keep both entries).

@trangevi

Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

…-install

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

Copilot AI commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved. Merged origin/main into the branch and kept both ensurepip and recordproxy entries in cli/azd/extensions/azure.ai.agents/cspell.yaml (commit 12ba75e).

@jongio jongio left a comment

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.

Incremental review: the merge from main resolved the cspell.yaml conflict (both ensurepip and recordproxy entries preserved). The pip-fallback venv logic doesn't interact with the predeploy/postdeploy migration (#8788) or model updates (#8789) merged since my last pass. Agents gate CI is still pending.

@trangevi trangevi enabled auto-merge (squash) June 25, 2026 23:01
@huimiu

huimiu commented Jun 26, 2026

Copy link
Copy Markdown
Member

/check-enforcer override

@trangevi trangevi merged commit d91d246 into main Jun 26, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext-agents azure.ai.agents extension

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Agents extension] azd ai agent run does not install into a venv when using pip

5 participants