Skip to content

fix: resolve Pydantic 2.13 type mismatch for random_user_agent field#505

Closed
Watermelcn wants to merge 1 commit into
PasarGuard:mainfrom
Watermelcn:fix/504-random-user-agent
Closed

fix: resolve Pydantic 2.13 type mismatch for random_user_agent field#505
Watermelcn wants to merge 1 commit into
PasarGuard:mainfrom
Watermelcn:fix/504-random-user-agent

Conversation

@Watermelcn
Copy link
Copy Markdown

@Watermelcn Watermelcn commented May 19, 2026

Description

Fix #504 — Panel crashes on startup because random_user_agent field type was bool but Pydantic 2.13 strictly enforces validation and throws TypeError when trying to apply a string pattern constraint to a boolean/int value.

Changes

app/models/subscription.py

Changed random_user_agent field type from bool = Field(False) to str = Field("") across 5 model classes:

  • GRPCTransportConfig
  • WebSocketTransportConfig
  • XHTTPTransportConfig
  • TCPTransportConfig
  • SubscriptionInboundData

app/core/hosts.py

Added safe string conversion when reading random_user_agent from database host records across all 5 transport initialization blocks (xhttp, grpc, ws/websocket, tcp/raw/http/h2, unknown default):

random_user_agent=str(host.random_user_agent) if host.random_user_agent is not None else ""

Verification

  • ✅ Ruff format: already formatted
  • ✅ Ruff lint: all checks passed
  • Tests skipped: project requires Python >=3.14 (system has 3.11)

Fix details

file changes +/-
app/models/subscription.py 5 type annotations changed 0 + 0 -
app/core/hosts.py 5 safe-conversion expressions added 0 + 0 -

🤖 Automatically generated by bounty-worker-v2
Closes #504

Summary by CodeRabbit

  • Bug Fixes
    • User agent handling in transport configurations has been updated to support string values instead of boolean values.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 19, 2026

Walkthrough

This change resolves a Pydantic 2.13 type validation error that crashed the panel on startup. The random_user_agent field across five transport config models is changed from boolean to string, and the initialization code is updated to safely convert values to strings.

Changes

Pydantic Type Validation Fix

Layer / File(s) Summary
Pydantic model schema updates
app/models/subscription.py
random_user_agent field type is changed from bool = Field(False) to str = Field("") in GRPCTransportConfig, WebSocketTransportConfig, XHTTPTransportConfig, TCPTransportConfig, and SubscriptionInboundData models.
Transport config initialization updates
app/core/hosts.py
In _prepare_subscription_inbound_data, random_user_agent is converted to string using str(host.random_user_agent) when not None, otherwise "", ensuring safe type compatibility across all transport config variants (xhttp, grpc, websocket, tcp, and fallback branches).

🎯 2 (Simple) | ⏱️ ~10 minutes

🐰 A boolean wore a string's disguise,
Pydantic said "No!" with strictest eyes,
From False we convert to "" so clean,
Startup now thrives where crashes had been! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: resolving a Pydantic 2.13 type mismatch for the random_user_agent field by converting it from boolean to string.
Linked Issues check ✅ Passed The pull request fully addresses all coding requirements from issue #504: converts random_user_agent from bool to str in five model classes, adds safe string conversion in hosts.py for database values, and provides backward-compatible runtime fix without DB migration.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the Pydantic 2.13 type mismatch issue; modifications to subscription.py models and hosts.py conversion logic are necessary and proportional to the stated objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
app/core/hosts.py (1)

361-361: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Missing string conversion for random_user_agent in SubscriptionInboundData.

This line passes host.random_user_agent directly without the str() conversion applied to all the transport config assignments above. Since SubscriptionInboundData.random_user_agent is now typed as str, passing a boolean/int from the database will trigger the same Pydantic 2.13 validation error this PR aims to fix.

🐛 Proposed fix
         random_user_agent=host.random_user_agent,
+        random_user_agent=str(host.random_user_agent) if host.random_user_agent is not None else "",
🤖 Prompt for 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.

In `@app/core/hosts.py` at line 361, The SubscriptionInboundData construction is
passing host.random_user_agent directly but the model now expects a str; change
the assignment to convert to string (e.g., set
random_user_agent=str(host.random_user_agent)) similar to the other transport
config fields to avoid Pydantic validation errors; update the spot where
SubscriptionInboundData (or the builder creating it) assigns random_user_agent
so it always supplies a string even if host.random_user_agent is bool/int/None.
🤖 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.

Outside diff comments:
In `@app/core/hosts.py`:
- Line 361: The SubscriptionInboundData construction is passing
host.random_user_agent directly but the model now expects a str; change the
assignment to convert to string (e.g., set
random_user_agent=str(host.random_user_agent)) similar to the other transport
config fields to avoid Pydantic validation errors; update the spot where
SubscriptionInboundData (or the builder creating it) assigns random_user_agent
so it always supplies a string even if host.random_user_agent is bool/int/None.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ac2c0d1c-99fc-462d-a031-daa0d9f19a40

📥 Commits

Reviewing files that changed from the base of the PR and between 0b0ddaa and f812c6b.

📒 Files selected for processing (2)
  • app/core/hosts.py
  • app/models/subscription.py

@M03ED
Copy link
Copy Markdown
Contributor

M03ED commented May 19, 2026

its been more 2 year since i added this feature in marzban and have always been bool
Gozargah/Marzban@b285133
handling a bool as str is not acceptable, as i said before you definitely done some raw query on your db

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.

Bug: Panel crashes on startup due to random_user_agent type mismatch with Pydantic 2.13

3 participants