Skip to content

fix(aimlapi): send attribution headers on every aimlapi.com request - #17491

Merged
yingfeng merged 1 commit into
infiniflow:mainfrom
aimlapi:feat/aimlapi-attribution-headers
Jul 28, 2026
Merged

yingfeng merged 1 commit into
infiniflow:mainfrom
aimlapi:feat/aimlapi-attribution-headers

Conversation

@eugeneBackDev

@eugeneBackDev eugeneBackDev commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The aimlapi.com provider added in #17311 does not identify itself on any of its outgoing requests, so its traffic cannot be attributed to the integration. This PR adds the two headers AIMLAPI expects — X-AIMLAPI-Source and X-AIMLAPI-Partner-ID — to every request the provider makes.

Change

  • common/aimlapi_utils.py (new) — a small helper that builds the header pair. It lives in common/ rather than rag/llm/ because api/apps/restful_apis/aimlapi_api.py is a blueprint loaded at API startup, and importing from the rag.llm package would pull in every model module with it.
  • rag/llm/{chat,embedding,cv}_model.py — the AIMLAPI adapters attach the headers to their OpenAI clients via with_options(default_headers=…) after super().__init__(), so the base class's timeout and retry wiring is untouched. Both the sync and async clients are covered, so streaming chat and async vision carry the headers too.
  • rag/llm/model_meta.py — the AIMLAPI model-meta sends them on its /v1/models catalog fetch.
  • api/apps/restful_apis/aimlapi_api.py — the two agent-authorization calls send them as well; the partner-id default moves to the shared helper so there is one source for it. The consent URL of the "Get API key" flow also carries the client identifier as a source query parameter: consent — and any sign-up that happens during it — runs in a browser on AIMLAPI's side, which a request header cannot reach.

Configuration — unchanged in spirit: production defaults are compiled in and both values stay overridable through the environment (AIMLAPI_SOURCE, AIMLAPI_PARTNER_ID), matching the existing AIMLAPI_* variables.

Testing — test/unit_test/common/test_aimlapi_utils.py covers the defaults, the environment overrides and a blank partner id. End to end, the headers were confirmed on the wire for chat, streaming chat, embeddings and the model-catalog fetch against the live API.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. python Pull requests that update python code labels Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 89f5a8d4-e8eb-4535-bf60-b00a0f3af1cd

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

A shared utility resolves AIMLAPI source and partner attribution from environment variables. Attribution headers are added to REST authorization flows, LLM clients, and model-list requests, with unit tests covering defaults and overrides.

Changes

AIMLAPI attribution integration

Layer / File(s) Summary
Attribution helpers and validation
common/aimlapi_utils.py, test/unit_test/common/test_aimlapi_utils.py
Adds environment-driven source and partner resolution, conditional header construction, and tests for defaults, trimming, overrides, and blank partner IDs.
REST authorization attribution
api/apps/restful_apis/aimlapi_api.py
Uses shared attribution helpers, adds headers to authorization and token-polling requests, and includes the source in the consent URL.
LLM client attribution
rag/llm/chat_model.py, rag/llm/cv_model.py, rag/llm/embedding_model.py, rag/llm/model_meta.py
Applies attribution headers to chat, vision, embedding, and raw model-list AIMLAPI requests, including timeout and non-success handling for model-list retrieval.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

A rabbit sent headers, neat and bright,
Through model paths by day and night.
Source trimmed clean, partners in tow,
REST and LLM flows now know.
“Hop!” said the hare, “the requests all show!”

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding attribution headers to AIMLAPI requests.
Description check ✅ Passed The description includes the required Summary section and provides clear change, configuration, and testing details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@rag/llm/model_meta.py`:
- Around line 566-575: Add structured logging in _get_raw_model_list for both
the missing URL early return and non-200 HTTP responses, including relevant
provider/request status context while excluding authorization headers, bearer
tokens, and other secrets. Preserve the existing None-return behavior and
successful JSON response handling.
- Around line 571-572: Update the AIMLAPI model-list request in the session.get
call to pass an explicit aiohttp.ClientTimeout bounded to the repository’s
60-second provider timeout pattern, while preserving the existing URL, headers,
and response handling.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3c3f3250-38e4-4982-bb3d-261dc35da396

📥 Commits

Reviewing files that changed from the base of the PR and between 7f21a7b and 7aaa60f.

📒 Files selected for processing (7)
  • api/apps/restful_apis/aimlapi_api.py
  • common/aimlapi_utils.py
  • rag/llm/chat_model.py
  • rag/llm/cv_model.py
  • rag/llm/embedding_model.py
  • rag/llm/model_meta.py
  • test/unit_test/common/test_aimlapi_utils.py

Comment thread rag/llm/model_meta.py Resolved
Comment thread rag/llm/model_meta.py Resolved
@eugeneBackDev
eugeneBackDev force-pushed the feat/aimlapi-attribution-headers branch from 7aaa60f to 8f53504 Compare July 28, 2026 11:24
Requests to aimlapi.com carried no client identification, so the provider's
traffic could not be attributed. Add a small shared helper that builds the
X-AIMLAPI-Source / X-AIMLAPI-Partner-ID pair and send it from every call
site: chat (sync + streaming), embeddings, vision, the /v1/models catalog
and the agent-authorization endpoints. Both values keep RAGFlow's defaults
and stay env-overridable (AIMLAPI_SOURCE, AIMLAPI_PARTNER_ID).

The consent URL of the "Get API key" flow carries the same client identifier
as a query parameter: consent (and any sign-up that happens during it) runs in
a browser on AIMLAPI's side, which a request header cannot reach.
@eugeneBackDev
eugeneBackDev force-pushed the feat/aimlapi-attribution-headers branch from 8f53504 to b209721 Compare July 28, 2026 11:27
@eugeneBackDev

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

Both findings are addressed in the latest push: the model-list fetch is now bounded by an explicit 60s aiohttp.ClientTimeout, and the missing-URL and non-200 paths log with context (no bearer token).

@coderabbitai

coderabbitai Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor
🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '--- PR diff for model metadata ---'
git diff --unified=80 HEAD^ HEAD -- rag/llm/model_meta.py || true

echo '--- Outline ---'
ast-grep outline rag/llm/model_meta.py --items all --match 'AIMLAPI|_get_raw_model_list|Model' || true

echo '--- AIMLAPI references and relevant request/logging context ---'
rg -n -C 10 'AIMLAPI|_get_raw_model_list|ClientTimeout|ClientSession|status|logger\.(warning|error|info)' rag/llm/model_meta.py

Length of output: 19508


@eugeneBackDev Confirmed. AIMLAPI._get_raw_model_list() now bounds the catalog request with a 60-second aiohttp.ClientTimeout, and both the missing-URL and non-200 paths provide useful context without logging authorization credentials. Both findings are addressed.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@yingfeng yingfeng added the ci Continue Integration label Jul 28, 2026
@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.65%. Comparing base (f4d1c9f) to head (b209721).
⚠️ Report is 72 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #17491   +/-   ##
=======================================
  Coverage   90.65%   90.65%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          650      650           
  Misses         39       39           
  Partials       28       28           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yingfeng
yingfeng merged commit c1e1d01 into infiniflow:main Jul 28, 2026
6 checks passed
buua436 pushed a commit to buua436/ragflow that referenced this pull request Jul 29, 2026
…nfiniflow#17491)

### Summary

The **aimlapi.com** provider added in infiniflow#17311 does not identify itself on
any of its outgoing requests, so its traffic cannot be attributed to the
integration. This PR adds the two headers AIMLAPI expects —
`X-AIMLAPI-Source` and `X-AIMLAPI-Partner-ID` — to every request the
provider makes.
Lookoff-AIMLAPI added a commit to aimlapi/ragflow-aimlapi that referenced this pull request Oct 19, 2026
…typed model rows

Review (Lynn-Inf):

- X-AIMLAPI-Source now honours AIMLAPI_SOURCE (default agent/ragflow), as
  common/aimlapi_utils.py did.
- HTTP-Referer and X-Title are dropped. They were never part of the infiniflow#17491
  contract, which is the two X-AIMLAPI-* headers.
- An override set to an empty value turns its header off, as
  os.environ.get(name, default) did in Python: os.LookupEnv tells "set to
  empty" apart from "unset", so an unset variable still sends the default
  while AIMLAPI_PARTNER_ID="" omits the partner header. The same holds for
  AIMLAPI_SOURCE.

The destination gate is unchanged: both headers go only to api.aimlapi.com.
A live chat through the driver carries exactly X-AIMLAPI-Source and
X-AIMLAPI-Partner-ID and nothing else.

ListModels no longer drops a row the gateway publishes without a type: it is
kept and typed from its name, like any other catalog miss. A typed row for
the same id still decides its family, and rows of a known family this driver
cannot call (image, video, audio, batches) are still dropped.

Tests: AIMLAPI_SOURCE honoured; an empty override omits its header without
touching the other; defaults when unset; HTTP-Referer and X-Title never sent,
to the gateway or to an overridden base URL; untyped rows kept and outranked
by a typed duplicate. Restoring each old behaviour fails its test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration python Pull requests that update python code size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants