feat(api): enhance max_tokens field with robust null handling

- Update LLMModelConfigUpdate schema to accept Union[int, str] for max_tokens
- Add field validator to gracefully handle various null representations:
  - Convert string "null" to null
  - Convert empty strings to null
  - Convert numeric strings to integers
  - Accept proper JSON null values
- Update documentation to reflect new behavior and explain robust null handling
- Prevents validation errors from incorrect null serialization by clients
This commit is contained in:
YueGuobin committed 2026-03-11 10:14:06 +08:00
1 parent e452f75ac3
commit 10a827f0c5
2 files changed
+25 -4

No files matched your search

@@ -14,8 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Optional, Literal
from pydantic import BaseModel, Field, ConfigDict
from typing import Optional, Literal, Union
from pydantic import BaseModel, Field, ConfigDict, field_validator
from uuid import UUID
from .base import DateTimeModelMixin
@@ -100,7 +100,7 @@ class LLMModelConfigUpdate(BaseModel):
model: Optional[str] = None
temperature: Optional[float] = Field(None, ge=0.0, le=2.0)
api_key: Optional[str] = None
max_tokens: Optional[int] = Field(None, gt=0)
max_tokens: Optional[Union[int, str]] = Field(None, description="Max tokens for generation (can be null)")
context_limit: Optional[int] = Field(None, gt=0, description="Model context window limit in K tokens (e.g., 128 = 128K tokens)")
context_strategy: Optional[Literal["conservative", "balanced", "aggressive"]] = Field(
None, description="Context trimming strategy"
@@ -113,6 +113,19 @@ class LLMModelConfigUpdate(BaseModel):
# Allow extra config fields
model_config = ConfigDict(extra="allow")
@field_validator('max_tokens', mode='before')
@classmethod
def validate_max_tokens(cls, v):
"""Handle string 'null' values for max_tokens."""
if v == "null" or v == "":
return None
if v is None:
return None
# Convert to int if it's a valid integer string
if isinstance(v, str) and v.isdigit():
return int(v)
return v
# Response schemas
class LLMModelConfigResponse(DateTimeModelMixin):