Skip to content

feat: ship inference workflows and read-only management (PTU deferred) - #239

Open
lzsweb wants to merge 31 commits into
mainfrom
dev/v2.2
Open

lzsweb wants to merge 31 commits into
mainfrom
dev/v2.2

Conversation

@lzsweb

@lzsweb lzsweb commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR is the single first-release integration entry point from
dev/v2.2 to main. It includes the inference/workflow additions originally
in #239 and the read-only ManagementClient merged through #240.

Maintainer review required: keep this PR open. Do not auto-merge to main
or publish a release.

First-release scope

All implemented features below ship together. PTU throughput reservation
is deferred in its entirety.
Unimplemented management writes and APIs not
opened by the service are not implicitly included.

Module Included features
Models Server-side filters, rich metadata, pagination, iter/list_all/is_available, region/workspace query context, read-only limits/permissions
Region Safe region passthrough, tenant/region-aware catalog cache and refresh, explicit opt-in RegionRouter discovery; no inference fallback
Batch Compatible-mode batch files, upload/query/download/delete, batch create/get/list/cancel/wait, JSONL validation, input-file reuse, output/error downloads
Batch Chat Dedicated non-streaming endpoint, long-wait timeout and no automatic retry by default
Responses create/get/delete, input_items pagination, streaming and final response
Conversations Conversation CRUD and item lifecycle, integrated with Responses
Messages Native Anthropic create/stream, tool calls, thinking/cache/output configuration and web-search marker handling
Compression fine_tunes.compress templates and jobs lifecycle, logs and waiter
Management Optional [management] extra, sync/async clients, RAM/STS credential chain, ACS3 signing, region binding, dual errors/pagination, redaction and raw response options
Management reads All 14 read ops: workspaces/models/limits/permissions (4), API keys (2), billing (2), TokenPlan account/members/seats/stats/subscription/shared packages (6)

Integration and PTU deferral

  • feat(management): read-only RAM ManagementClient (G1/G2/G5) #240 was retargeted to dev/v2.2 and merged normally at 0f376e1.
  • PTU deferral is committed on dev/v2.2 at 9d99a0c.
  • Removed PTU capacity resources/types and the deployment PTU convenience
    APIs (create fields, scale fields, renew and overflow configuration).
  • Kept ordinary deployment/MU/LoRA behavior and non-empty/percent-encoded ID
    checks. Deferred PTU requests are rejected before sending, including via
    extra_body and raw=True.
  • Kept model catalog metadata/filters that describe PTU support; discovery
    does not purchase or manage capacity.
  • PTU implementation history remains recoverable from the earlier commits.

Correctness fixes retained

  • Management strict validation errors redact body, response and exception
    diagnostics, not only successfully parsed SecretStr fields.
  • HTTP failures cannot be overridden by a business success=True flag.
  • Injected HTTP-client default query parameters are merged before signing;
    the signed canonical URL is preserved on the wire.
  • Region isolation, sync/async symmetry, error envelopes, raw options,
    SDK request identifiers, pagination and non-idempotent retry protections
    remain part of the corresponding first-release functionality.

Validation

  • Targeted deployment and management regressions: 239 passed.
  • Changed-file Black, flake8, mypy and pylint checks passed.
  • Integrated 2.0 full suite: 6294 passed / 10 skipped (Python 3.13).
  • Integrated legacy compatibility suite: 751 passed / 42 skipped.
  • Remote CI on 9d99a0c: unit tests on Python 3.9/3.11/3.13,
    legacy tests on 3.11, pre-commit checks and docs build all passed.
    Docs deployment was skipped under the PR conditions; no release
    was deployed.
  • Live endpoint acceptance: requires controlled API-key/workspace and
    RAM/STS credentials; none are configured in this environment.
  • Maintainer manual review of feat: ship inference workflows and read-only management (PTU deferred) #239 before any merge to main.

No PTU purchase, renewal or scale was performed. Mock tests and missing-
credential skips are not evidence of successful live endpoint acceptance.
The legacy suite emits existing unclosed aiohttp-session warnings; its
assertions passed and no legacy source was changed.

zhansheng.lzs and others added 29 commits November 17, 2026 16:37
Route the singular `provider` into the server-side `providers` filter and
pass through the full 12-filter contract (name/model/language/providers/
inference_providers/capabilities/features/context_window/service_site/
supports/deployment_methods/deployment_ptu_service_tiers), with array
filters sent as repeated query params. This fixes the client-side per-page
slice that left total_count pre-filter and hid matches on other pages;
filters are threaded through bind_pagination so traversal preserves them.

Type the rich catalog metadata (ModelPrice/ModelPriceRange/InferenceMetadata/
ModelInfo and the extended Model fields). Update CLI --provider help and the
example to the server-side semantics; rewrite the provider tests and add
coverage for repeated keys, scalar passthrough, metadata parsing, and
filter-preserving pagination (sync/async).
Wire client.fine_tunes.compress against /api/v1/fine-tunes/compress:
templates catalog (base_models + custom_models[].templates[]) and the
jobs lifecycle (create/get/list/logs/cancel/delete/wait), sync + async.
Matches the official Model Studio compression contract: quantization-only,
integer GPU-seconds usage, server-side job filters, terminal-state waiter.
… (B1)

Mount the OpenAI-compatible File plane for the Batch workflow under
client.batches.files: upload (purpose=batch, JSONL-validated), content
download, cursor-paginated list (purpose/after/limit), get and delete.
Every call routes through compatible_mode_base_url (/compatible-mode/v1/files)
independent of the chat protocol, and carries sdk_module="batches" on the
x-dashscope-sdk-client header.

This is a distinct wire from the native client.files (fine-tune): multipart
"file" singular, flat OpenAI objects, after/has_more cursor pagination (new
BatchFileList, since BasePage is offset-only). Native client.files is left
untouched; B2 adds the batch job plane to the same Batches class.

Sync/async symmetric with raw overloads; 34 mock tests cover wire shape,
cursor traversal, content bytes/write_to_file, error mapping and routing.
…it (B2)

Extend the Batches resource with the OpenAI-compatible batch job plane:
create, get, cursor-paginated list, cancel, a polling wait, and a pre-upload
validate_input row-level JSONL check. Same compatible-mode wire as B1's File
plane (/compatible-mode/v1/batches, protocol-independent, sdk_module="batches"
on x-dashscope-sdk-client).

create has no server-side idempotency key, so it defaults to max_retries=0
(no auto-retry) to avoid spawning duplicate batches; get/list/cancel/wait keep
the default retry budget. No fake idempotency header is injected -- a caller
passing max_retries explicitly owns the duplicate risk. completion_window is
validated client-side (integer + h/d, 24h..336h).

New models Batch/RequestCounts/BatchErrors/BatchErrorItem/BatchList. BatchList
and B1's BatchFileList now share a _CursorPage base (cursor traversal with
polymorphic-self typing); the Batch list limit is [1,100] (vs File [1,2000])
and advances by last_id. wait mirrors fine_tunes.wait with lowercase terminal
statuses {completed,failed,expired,cancelled}.

Sync/async symmetric with raw overloads; 64 mock tests cover wire shape, the
no-retry default, cursor traversal, waiter terminal/timeout/error paths,
validate_input row checks, error mapping and routing.
…ist + stream (C1)

First-class OpenAI-compatible Responses resource on the core inference
Client (API-Key Bearer), independent of chat protocol normalization.

- types/responses.py: flat OpenAI response object; single permissive
  ResponseOutputItem covering all 10 heterogeneous output item types;
  two-generation usage union + tool_usage; 8 typed tool classes;
  ResponseDeleted {id, deleted} (no object); InputItem; stream events.
- resources/responses.py: create/get/delete + input_items.list; sync/async
  symmetric; ResponseStream/AsyncResponseStream over transport SSE with
  text_stream/reasoning_stream/get_final_response; InputItemList reuses
  the shared _CursorPage (cursor prefers last_id, created_at in ms).
- Bailian extensions as first-class params: reasoning.effort/summary,
  prompt_cache_options, ocr_options, session_cache -> x-dashscope-session-cache
  header (create-level); enable_thinking DeprecationWarning (once-guard).
- Pre-flight validation: model/input required, previous_response_id XOR
  conversation, function_call_output must immediately follow its
  function_call with matching call_id.
- Routing via compatible_mode_base_url; sdk_module="responses" on every
  request_api/open_stream; local _checked mirrors batches/decision
  dual-layer error handling with non-raising envelope backfill.
- create three overloads (stream/raw); get/delete/list support raw=True.
- tests/test_responses.py: 33 mock tests (httpx.MockTransport).
…ontext (A2)

Build on A1's server-side filters + rich metadata to make the model
catalog fully traversable and region-aware.

- iter()/list_all() (sync+async): walk every page of the filtered catalog
  from page 1 to total, reusing BasePage.iter_pages/iter_pages_async and
  forwarding all filters per page. iter is lazy (Iterator/AsyncIterator),
  list_all eager (List[Model]); page_size defaults to 100 to cut
  round-trips. Current Client Region only.
- is_available(model) (sync+async) -> bool: answers only for this Client's
  Region/endpoint, never queries other Regions implicitly. Mirrors get's
  wire call; transport/HTTP errors propagate rather than reading as
  "unavailable"; empty/blank model id raises ValueError so an empty filter
  cannot masquerade as the whole catalog matching.
- Model gains SDK-injected served_region (=config.region) and workspace
  (=effective workspace, per-call override wins), stamped on every model
  in list/get (incl. the not-found envelope branch). These describe the
  endpoint the catalog was read from, so the SDK values are authoritative.
- service_site kept distinct from served_region: verified against the
  official list-models doc that the wire per-model object returns neither
  service_site nor region/workspace. service_site is a deployment-site
  query filter, so Model.service_site echoes the query's filter (None when
  unfiltered) and preserves a wire-provided value when present.
- tests: 17 new cases in test_files_models.py (injection/default region/
  workspace override/service_site echo + wire precedence/get + not-found/
  iter all-pages + filter preservation + laziness/list_all/is_available
  true/false/empty-model/HTTP-error/sdk header/async symmetry);
  test_pagination dump expectation updated for the three new fields.
…resources (A3)

Wire-verified against Bailian docs: GET /models/limits -> output.quotas[]
and GET /models/permissions -> output.permissions[] (API-Key bearer,
inference plane). Corrections: versions has no public endpoint (dropped);
authorizations renamed to permissions per wire authority. Read-only list/get
with BasePage pagination; writes deferred to management plane (G3).
…put reuse e2e (B3)

Batch-level convenience over the existing files.content download: resolve a
batch's output_file_id/error_file_id (fetching the batch first when given an
id) and return Optional[FileContent], None when the file is absent. Input
file reuse is already supported by create/files.list and now covered by
tests. Adds a gated live e2e using the public zero-cost batch-test-model.
Part 1: downgrade the static region whitelist to safe DNS-label
passthrough. Any region/workspace interpolated into the gateway host
must match an RFC1123 label (host-injection guard); with a workspace an
unknown-but-safe region warns instead of raising, without a workspace a
known shared-gateway mapping is still required, and an explicit base_url
bypasses the policy. CLI region validation mirrors the new acceptance.

Part 2: per-instance short-TTL (60s) catalog cache for list/get/
is_available, keyed by region/workspace/credential-fingerprint/query so
entries never cross tenants or Regions. refresh=True forces a fetch,
raw=True and errors are never cached, and only genuine successes are
stored. Adds refresh to list/get/iter/list_all/is_available (sync+async).
…border compliance notice (A5)

Discovery-only cross-region model availability (no inference routing,
no auto fallback). Disabled by default: construction requires explicit
allow_cross_region=True; enabling warns that cross-border data
compliance is the user's sole responsibility. Validates per-region
Clients as one config unit (region/workspace/key), reuses A4 catalog
cache so each region costs one network read, and records per-region
errors without aborting or silently switching regions.
…hat (B4)

OpenAI-compatible single non-streaming request on the dedicated batch
gateway (batch.dashscope.aliyuncs.com/compatible-mode/v1), independent of
the client's chat protocol. Reuses OpenAIProtocol + ChatCompletion +
normalize_messages; derives its own batch base_url (explicit base_url
override as escape hatch). timeout 60..3600s (default 3600), max_retries
passthrough for unscheduled-timeout retries. sdk_module=batch_chat
(/batch_chat2). 27 mock tests; full suite 6080 passed.
OpenAI-compatible Conversations API on the compatible-mode wire, reusing
C1 conventions: compatible_mode_base_url routing (chat protocol cannot
move it), per-resource _checked dual-layer error envelope, _CursorPage
cursor pagination, sdk_module=conversations (/conversations2), sync/async
symmetry, local pre-validation (items<=20, metadata<=16 pairs, limit
1-100). Full documented surface: conversation create/get/update/delete +
items.create/list/get/delete. Types exported via dashscope.types (the
top-level dashscope.Conversation remains the 1.x class). Links to
Responses via conversation= (mutually exclusive with previous_response_id).
40 mock tests; full suite 6120 passed.
Add client.messages (sync/async) as a first-class Anthropic Messages
resource, distinct from the chat protocol=anthropic adapter.

- types/anthropic_messages.py: AnthropicMessage/ContentBlock/Usage/
  StreamEvent + server-tool/cache/thinking/output configs, exported via
  dashscope.types (avoids collision with canonical chat Message).
- resources/messages.py: create + stream overloads, _checked dual-layer
  error envelope, temperature [0,2) validation, max_tokens required,
  budget_tokens deprecation warning, sdk_module=messages (/messages2),
  anthropic-version header.
- _config.anthropic_base_url: POST {base}/apps/anthropic/v1/messages
  (base not ending /v1); explicit base_url stays service root.
- Stream accumulation via _MessageBuilder (Anthropic has no terminal
  full-message event); server_tool_use/web_search_tool_result carry
  complete input/content at content_block_start (non-incremental).
- Shared web_search billing-header injection helpers in
  protocols/anthropic.py so native resource and chat adapter behave
  identically; output_config/cache_control/server tools first-class.

count_tokens, /v1/models, metadata first-class intentionally not done
(not provided / not documented by Bailian).

51 mock tests + 6 adapter consistency tests; full suite 6177 passed.
…erations (D3)

Extend client.deployments with plan="ptu" create/scale fields, renew and
update_overflow_strategy, and mount capacity_instances (create/list/get/
scale/renew/delete) + capacity_operations (get/wait) subresources.

Billing safeguards per the authoritative throughput-reservation contract:
purchase/renew/scale/delete default to max_retries=0, instance writes carry
an x-acs-req-uuid idempotency key mirrored to X-DashScope-RequestId (the
first ModelCode create deliberately does not), and HTTP-200
operation_status=FAILED is surfaced as a catalog error. CapacityOperation
preserves the original write request_id separately from the poll envelope;
instance pagination normalizes the camelCase wire shape.

D2 (fine_tune metrics/retry) and D4 (deployments start/stop/rollout) verified
not open in the official docs and left out of scope.
…branches (D3)

Run pre-commit (black 79-col, add-trailing-comma, flake8, mypy, pylint) on
the D3 changed files. Refactor _build_create_body to collapse optional-field
branches into a dict update and extract _validate_ptu_create, clearing
pylint R0912 (too-many-branches). No behavior change; 171 tests still green.
The identifier value is a Bailian doc convention; riding it as a standalone
system text block is a valid system form, not a doc-mandated packaging.
The A–D milestone files were committed without a full pre-commit run, so
CI failed. Apply black formatting and resolve the genuine lint errors:

- flake8/pylint: drop unused httpx / NotFoundError imports and unused r2.
- pylint R1735: dict(...) -> dict literals (models, fine_tunes_compress).
- pylint R0912: extract helpers for branchy functions — models
  _build_list_params filter collapse, batches _validate_batch_row,
  messages _MessageBuilder per-event handlers + shared finalizer.
- mypy: annotate filter_kwargs Dict[str, Any] (stops **unpack overload
  explosion); guard Optional[int] content-block indexes.
- pylint W0622: scope redefined-builtin disable to Responses.create
  overloads (canonical `input` API param, matching the impl precedent).
- pylint E1133: materialize Optional[Any] content via list() so astroid
  sees an iterable in anthropic_messages.search_results.

Behavior preserved: full suite 6216 passed, 47 skipped.
Trim the `current` job matrix to the odd CPython legs (drop 3.10/3.12);
the archived `legacy` job stays on 3.11.
- messages: preserve message_start usage counters when a spec-conformant
  delta omits them (exclude_unset over exclude_none), and honor the
  client's strict_validation when building the final streamed message
- models: bypass the catalog cache for requests carrying extra_query /
  extra_headers so overrides neither read nor pollute cached entries;
  when the gateway omits total, mark the page's total unknown and keep
  walking full pages instead of truncating after the first page
- deployments/PTU: reuse a caller-supplied idempotency key regardless of
  header case, emitting one x-acs-req-uuid / X-DashScope-RequestId pair;
  accept snake_case pagination aliases on capacity instance lists
- batch_chat: honor client protocol_options (strict mode) when building
  request bodies instead of a shared default protocol
- batches/files: skip blank lines in JSONL pre-validation to match the
  batches row scanner, and reject all-blank batch uploads like
  validate_input does

Behavior note: models.list's singular provider filter now folds into the
server-side providers filter; it no longer matches source client-side,
and total_count reflects the filtered set (documented in list()).
…1/G2/G5)

Add dashscope.management as an optional [management] extra: a ToB
self-service backend integration layer over the ModelStudio/2026-02-10
RAM OpenAPI (ROA). Read-only first release.

- G1 atomic layer: 14 read ops only (workspaces/models/limits/permissions,
  api keys, billing overview/trend, token plan account/members/seats/
  subscription/shared-packages). 24 write/out-of-scope ops deferred and
  catalogued as high-risk in md; no code.
- G2 resources: ManagementClient/AsyncManagementClient with sync+async
  symmetry, raw=True overloads, region binding + with_options(region).
- G5 cross-cutting: V3 ACS3-HMAC-SHA256 signing via official
  alibabacloud_openapi_util; credential chain via alibabacloud_credentials;
  dual-envelope success gate + RAM error crosswalk; SecretStr redaction;
  dual pagination (TokenPage nextToken / BasePage PageNo).
- Every request carries x-dashscope-sdk-client=python-sdk/<ver>/management2
  + session id + client-request-id, folded into the signed header set.

Tests: 120 mock tests (signer gateway oracle, credentials, errors,
transport, resources, pagination) + 5 env-gated read-only e2e. Full suite
6498 passed / 51 skipped / 0 failed. pre-commit green.
- Add _path_id utility to validate non-empty string IDs and percent-encode
- Apply _path_id to deployed_model, instance_id, operation_id in deployments module
- Add similar ID guards and encoding for file_id and batch_id in batches module
- Add job_id validation and encoding in fine_tunes_compress module
- Enforce model ID non-empty validation in models module for list, get, limits, permissions
- Modify batch_chat create to default max_retries to 0 to avoid duplicate billing
- Add tests for ID validation errors before sending requests
- Add tests verifying correct percent-encoding of IDs in request URLs
- Adjust max_retries default for batch_chat and fine_tunes_compress create operations to 0
- Ensure that unsafe characters do not retarget URL segments by encoding them
- Import urllib.parse.quote to encode IDs in URLs
- Encode conversation_id, item_id, and response_id to prevent path retargeting by '/', '?', or '#'
- Reject empty or whitespace-only IDs before making requests
- Add synchronous and asynchronous tests to verify percent-encoding of IDs in request paths
- Ensure requests with invalid IDs do not reach the network layer and raise ValueError early
- Verify correct encoding is applied consistently across conversation, item, and response API calls
…identity swap

- Raise error if partial access_key_id or access_key_secret is provided alone
- Prevent fallback to ambient host credentials when partial AK is detected
- Convert numeric error codes to string to avoid AttributeError in error mapping
- Improve secret redaction by matching sk- tokens with regex and applying filter on management loggers
- Introduce get_management_logger to attach secret redaction filter to all management loggers
- Limit transparent retries to safe HTTP methods to avoid duplicate side effects on write ops
- Support per-call timeout overrides and preserve injected client timeout settings in transport
- Add tests for partial explicit credential rejection, management logger redaction,
  retry behavior on write methods, and timeout handling with client and per-call overrides
- Removed unnecessary newlines in assertion statements
- Improved readability by simplifying multiline assertions
- Ensured consistent assert formatting in test_management_logger_carries_redaction_filter
feat(management): read-only RAM ManagementClient (G1/G2/G5)
@lzsweb lzsweb changed the title feat: fill Bailian control-plane API gaps (modules A–D) feat: ship inference workflows and read-only management (PTU deferred) Nov 23, 2026
luk384090-cloud added 2 commits November 23, 2026 14:23
…eters

- Rename variables to use raw IDs when paginating to avoid double encoding
- Percent-encode job IDs in fine-tunes compress API to safely handle special chars
- Validate and reject empty or whitespace-only job IDs before making requests
- Update paginated list methods to maintain raw IDs in after cursor parameters
- Add support for extra_body option to bypass caching in models API
- Ensure logs endpoints echo raw job ID instead of encoded form
- Add multiple sync and async tests verifying no double encoding in pagination
- Refactor internal API calls to encode IDs exactly once for consistent behavior
- Introduce GitHub Actions workflow for MkDocs Material site build and deployment
- Enable docs build on PRs, pushes to main, and release publishing events
- Add hooks.py to provide dynamic substitutions for SDK version and release notes
- Implement localized rendering of release notes with support for English and Chinese
- Add release-notes.md in English and Chinese with changelog overview and 2.0 highlights
- Refine sidebar navigation and prune 404 pages from sitemap for i18n builds
- Re-export applications subpackage for improved type checking and documentation cohesion

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant