flink-agents icon indicating copy to clipboard operation
flink-agents copied to clipboard

[Bug][api][python] YAML loading leaves partially registered resources after a chat model / model router name clash

Open Assertive-xxh opened this issue 3 weeks ago • 4 comments

Search before asking

  • [x] I searched in the issues and found nothing similar.

Description

#1366 made Java loadYaml check registration conflicts before it changes the environment. Python load_yaml already has a pre-check, but it only looks for a duplicate name within the same resource type. The rule that a chat model and a model router cannot share a name is checked later, inside env.add_resource, while the file is being committed. When that check fails, the resources committed before it stay registered.

I hit two cases:

  1. The environment already has a chat model assistant. Loading a file that declares a prompt fresh and a model router assistant fails with Resource 'assistant' cannot name both a chat model and a model router. The prompt fresh is registered anyway. After renaming the router, loading the same file again fails with Duplicate shared prompt 'fresh'.
  2. A single file declares a shared chat model and a shared model router with the same name. Loading fails with the same message, and the chat model and the prompts from that file stay registered.

Java covers case 1 since #1366 (sharedChatModelNameClashWithRouterLeavesFailedFileUnregistered).

Expected behavior: a file that fails on this clash registers nothing, the same as in Java.

How to reproduce

Case 1, candidate.yaml:

prompts:
  - name: fresh
    text: hello
model_routers:
  - name: assistant
    clazz: router
    candidates: [small]
    strategy_type: rule_based
env.add_resource(
    "assistant", ResourceType.CHAT_MODEL, ResourceDescriptor(clazz="some.Setup")
)

load_yaml(env, "candidate.yaml")
# ValueError: Resource 'assistant' cannot name both a chat model and a model router.

env.resources[ResourceType.PROMPT]
# {'fresh': ...}  <- registered despite the failure

# rename the router to "picker" in candidate.yaml, then:
load_yaml(env, "candidate.yaml")
# ValueError: Duplicate shared prompt 'fresh' (loading candidate.yaml)

Case 2, loaded into an empty environment:

prompts:
  - name: fresh
    text: hello
chat_model_setups:
  - name: same
    clazz: ollama
model_routers:
  - name: same
    clazz: router
    candidates: [small]
    strategy_type: rule_based

Loading fails with the same ValueError, and afterwards env.resources holds the chat model same and the prompt fresh.

I reproduced both with the mocked environment that test_loader.py uses (_registration_env), without submitting a job.

A possible fix is to call check_model_name_collision in the pre-check loop of load_yaml, once against env.resources and once against the file's own shared resources. I tried that locally and the existing YAML tests still pass.

Version and environment

Flink Agents: main at 656b1c8 (1.0.dev0) PyFlink: 2.3.0 Python: 3.12.3 OS: Ubuntu 24.04 (WSL2)

Are you willing to submit a PR?

  • [x] I'm willing to submit a PR!

Assertive-xxh avatar Nov 13 '26 16:11 Assertive-xxh

Thanks @Assertive-xxh for the clear repro and the suggested pre-check. This looks like a good Python parity follow-up to #1366.

I'd be happy to take this if you aren't already preparing a PR: add check_model_name_collision to the load_yaml pre-check (against env.resources and the file's own shared resources) and tests for both cases you described. If you're already working on it, please go ahead and I'll stand down.

@wenjin272, if that direction looks right, a quick confirm would be appreciated before I open a PR.

HsbcJone avatar Nov 14 '26 00:11 HsbcJone

Hi @HsbcJone, thanks for taking a look and for offering to help. I appreciate it.

I'd like to take this one myself. The fix is already working locally, and I'm turning my repro into tests for both cases. I'll open the PR as soon as a committer confirms the direction (cc @wenjin272).

Since you wrote the router YAML support in #1375, I'd really value your review once the PR is up.

Assertive-xxh avatar Nov 14 '26 00:11 Assertive-xxh

I went ahead and opened #1388 so the fix and tests are easy to look at.

@wenjin272, I'm happy to change the approach if you'd prefer a different direction.

@HsbcJone, your review is welcome whenever you have time.

Assertive-xxh avatar Nov 14 '26 07:11 Assertive-xxh

Thanks @Assertive-xxh for reporting this and opening #1388. The pre-check approach makes sense. I think it would be good to address this in both Python and Java so their behavior stays aligned: #1366 already covers Java conflicts with resources registered in the environment, but the shared chat model/router name clash within the same file is still missing.

@HsbcJone, thanks for offering to help! Since @Assertive-xxh already has the fix ready and has opened a PR, I'll assign this one to them. Your review would be very welcome, especially given your work on the router YAML support.

wenjin272 avatar Nov 16 '26 02:11 wenjin272