Skip to content

Link Prompts and Experiments when prompts are loaded/registered#18883

Merged
TomeHirata merged 11 commits intomlflow:masterfrom
TomeHirata:feat/prompt-experiment-link
Dec 2, 2025
Merged

Link Prompts and Experiments when prompts are loaded/registered#18883
TomeHirata merged 11 commits intomlflow:masterfrom
TomeHirata:feat/prompt-experiment-link

Conversation

@TomeHirata
Copy link
Collaborator

@TomeHirata TomeHirata commented Nov 18, 2025

🛠 DevTools 🛠

Open in GitHub Codespaces

Install mlflow from this PR

# mlflow
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/18883/merge
# mlflow-skinny
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/18883/merge#subdirectory=libs/skinny

For Databricks, use the following command:

%sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/18883/merge

Related Issues/PRs

n/a

What changes are proposed in this pull request?

Currently linkage to prompt version is tracked for runs, spans, and logged models. In order to allow users to group prompts by experiments, we want to track the linkage between experiments and prompts.

Changes:

  • Create the linkage between experiments and prompts when load_prompt or register_prompt is called using prompt tags

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Does this PR require documentation update?

  • No. You can skip the rest of this section.
  • Yes. I've updated:
    • Examples
    • API references
    • Instructions # I'll file a follow up with

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/tracking: Tracking Service, tracking client APIs, autologging
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/evaluation: MLflow model evaluation features, evaluation metrics, and evaluation workflows
  • area/gateway: MLflow AI Gateway client APIs, server, and third-party integrations
  • area/prompts: MLflow prompt engineering features, prompt templates, and prompt management
  • area/tracing: MLflow Tracing features, tracing APIs, and LLM tracing functionality
  • area/projects: MLproject format, project running backends
  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages

How should the PR be classified in the release notes? Choose one:

  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

Should this PR be included in the next patch release?

Yes should be selected for bug fixes, documentation updates, and other small changes. No should be selected for new features and larger changes. If you're unsure about the release classification of this PR, leave this unchecked to let the maintainers decide.

What is a minor/patch release?
  • Minor release: a release that increments the second part of the version number (e.g., 1.2.0 -> 1.3.0).
    Bug fixes, doc updates and new features usually go into minor releases.
  • Patch release: a release that increments the third part of the version number (e.g., 1.2.0 -> 1.2.1).
    Bug fixes and doc updates usually go into patch releases.
  • Yes (this PR will be cherry-picked and included in the next patch release)
  • No (this PR will be included in the next minor release)

- Introduced functionality to link prompt versions to experiments, allowing for better organization and tracking of prompts within specific experiments.
- Updated the `AbstractStore` and its SQLAlchemy implementation to include methods for adding and retrieving prompt-experiment associations.
- Enhanced the `MlflowClient` to facilitate linking prompts during registration and loading processes.
- Added tests to ensure correct linking behavior and to verify that prompts can be associated with multiple experiments without duplication.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
- Updated the prompt linking mechanism to tag prompts with associated experiment IDs instead of creating entity associations.
- Modified the `AbstractStore` and its SQLAlchemy implementation to remove methods related to prompt-experiment associations.
- Enhanced the `MlflowClient` to manage experiment ID tags during prompt registration and loading.
- Updated tests to verify the new tagging behavior and ensure idempotency when tagging prompts with the same experiment ID.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
…tore class

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
…es and update search_prompts function to return a list instead of PagedList. Enhance docstring for clarity on prompt search functionality.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
@github-actions github-actions bot added v3.6.1 area/prompts MLflow Prompt Registry and Optimization rn/feature Mention under Features in Changelogs. labels Nov 18, 2025
@TomeHirata TomeHirata requested a review from Copilot November 18, 2025 08:05
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Copy link
Contributor

Copilot AI left a comment

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 adds the ability to link prompts to experiments when prompts are loaded or registered within an active MLflow run. Experiment IDs are stored as comma-separated values in a special prompt tag, enabling users to search for prompts by experiment.

  • Tracks experiment linkage via PROMPT_EXPERIMENT_IDS_TAG_KEY tag when load_prompt or register_prompt is called within an active run
  • Implements experiment_id filter syntax for search_prompts() API that translates to tag-based filtering
  • Adds integration tests covering prompt-experiment linking and search functionality

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/genai/prompts/test_prompts.py Adds comprehensive integration tests for prompt-experiment linking and searching by experiment ID
mlflow/tracking/client.py Updates register_prompt to link prompts to experiments and enhances search_prompts documentation with experiment_id filter examples
mlflow/tracking/_model_registry/fluent.py Updates load_prompt to link prompts to experiments, improves search_prompts documentation, and fixes deprecated _logger.warn usage
mlflow/store/model_registry/abstract_store.py Implements _parse_experiment_id_filter to transform experiment_id filter syntax to tag-based LIKE queries
mlflow/store/_unity_catalog/registry/rest_store.py Integrates _parse_experiment_id_filter into Unity Catalog search_prompts implementation
mlflow/prompt/constants.py Adds PROMPT_EXPERIMENT_IDS_TAG_KEY constant for storing experiment IDs in prompt tags
mlflow/entities/entity_type.py Adds PROMPT entity association type for consistency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Updated the prompt linking mechanism to ensure prompts are tagged with associated experiment IDs during registration and loading.
- Refactored the `MlflowClient` to manage experiment ID tags more effectively, including asynchronous handling for linking prompts to experiments.
- Improved SQL injection protection in the `AbstractStore` by escaping backslashes in experiment IDs.
- Updated tests to verify the new tagging behavior and ensure correct linking of prompts to experiments without duplication.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
@github-actions
Copy link
Contributor

github-actions bot commented Nov 18, 2025

Documentation preview for c1d4e23 is available at:

More info
  • Ignore this comment if this PR does not change the documentation.
  • The preview is updated when a new commit is pushed to this PR.
  • This comment was created by this workflow run.
  • The documentation was built by this workflow run.

# Use LIKE to match the experiment ID and experiment ID is auto-incremented
# integer. So add comma before and after the list of experiment IDs to
# avoid false matches (e.g., "1" matches "10").
exp_ids = f",{exp_ids},"
Copy link
Collaborator Author

@TomeHirata TomeHirata Nov 18, 2025

Choose a reason for hiding this comment

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

This is not very beautiful, but I couldn't think of a better way to store and query a list type field without changing the schema of RegisteredModel. EntityAssociation is not really applicable for Prompts since it's managed by model registry store while EntityAssociation is in tracking store.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense, I think it is inevitable. Can we make sure this update is thread-safe and does not affect performance (I guess so cuz we file is async, but we should make sure no memory leak).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good, added a lock and moved the linkage logic so that it's called only when there's cache miss.

@TomeHirata TomeHirata requested a review from B-Step62 November 18, 2025 10:09
# Use LIKE to match the experiment ID and experiment ID is auto-incremented
# integer. So add comma before and after the list of experiment IDs to
# avoid false matches (e.g., "1" matches "10").
exp_ids = f",{exp_ids},"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense, I think it is inevitable. Can we make sure this update is thread-safe and does not affect performance (I guess so cuz we file is async, but we should make sure no memory leak).

@B-Step62
Copy link
Collaborator

B-Step62 commented Nov 20, 2025

/review check type hints and general coding standard


🚀 Review workflow started


✅ Review completed.

Review Output

Perfect! I've completed the PR review and added comments for all the issues I found. Let me summarize what I found:

PR Review Summary

I reviewed PR #18883 "Link Prompts and Experiments when prompts are loaded/registered" and found 3 issues that should be addressed:

Issues Found:

  1. Security vulnerability (mlflow/store/model_registry/abstract_store.py:586): The LIKE pattern construction is vulnerable to SQL injection through wildcard injection. Since experiment IDs are auto-incremented integers, the code should validate that the experiment_id is numeric before using it in the query.

  2. Documentation inconsistency (mlflow/tracking/_model_registry/fluent.py:667): The return type was changed from PagedList[Prompt] to list[Prompt], but the docstring still mentions pagination features like .token attribute. The documentation needs to be updated to match the actual return type.

  3. Test reliability issue (tests/genai/prompts/test_prompts.py:1331): Multiple tests use time.sleep(1) to wait for async threading operations, which makes tests slower and potentially flaky. The codebase already has a join_thread_by_name_prefix() helper function that should be used instead for more reliable synchronization.

All comments have been posted to the PR with specific suggestions for fixes.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
… to experiments. Added a fixture to ensure linkage threads complete in tests.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Signed-off-by: TomuHirata <tomu.hirata@gmail.com>
Copy link
Collaborator

@B-Step62 B-Step62 left a comment

Choose a reason for hiding this comment

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

LGTM!

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
@TomeHirata TomeHirata enabled auto-merge December 2, 2025 01:16
@TomeHirata
Copy link
Collaborator Author

The failures are related to the conda issue.

@TomeHirata TomeHirata disabled auto-merge December 2, 2025 04:46
@TomeHirata TomeHirata merged commit b3f0e30 into mlflow:master Dec 2, 2025
43 of 47 checks passed
BenWilson2 pushed a commit to BenWilson2/mlflow that referenced this pull request Dec 4, 2025
…ow#18883)

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Signed-off-by: TomuHirata <tomu.hirata@gmail.com>
BenWilson2 pushed a commit that referenced this pull request Dec 4, 2025
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Signed-off-by: TomuHirata <tomu.hirata@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/prompts MLflow Prompt Registry and Optimization rn/feature Mention under Features in Changelogs. v3.6.1 v3.7.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants