Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions vllm/sampling_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import copy
import json
from dataclasses import field
from dataclasses import dataclass
from enum import Enum, IntEnum
from functools import cached_property
from typing import Annotated, Any

import msgspec
from pydantic.dataclasses import dataclass

from vllm.config import ModelConfig, SpeculativeConfig, StructuredOutputsConfig
from vllm.exceptions import VLLMValidationError
Expand Down Expand Up @@ -46,13 +45,12 @@ class StructuredOutputsParams:
whitespace_pattern: str | None = None
structural_tag: str | None = None

_backend: str | None = field(default=None, init=False)
"""CAUTION: Should only be set by Processor._validate_structured_output"""
_backend_was_auto: bool = field(default=False, init=False)
"""CAUTION: Should only be set by Processor._validate_structured_output"""

def __post_init__(self):
"""Validate that some fields are mutually exclusive."""
# CAUTION: Should only be set by Processor._validate_structured_output
self._backend: str | None = None
self._backend_was_auto: bool = False

count = sum(
[
self.json is not None,
Expand All @@ -74,6 +72,17 @@ def __post_init__(self):
f"but none are specified: {self.__dict__}"
)

def __replace__(self, **changes):
"""Custom replace that preserves instance attributes not in dataclass fields."""
from dataclasses import replace

new_obj = replace(self, **changes)
# Preserve _backend and _backend_was_auto which are set in __post_init__
# but not part of the dataclass fields, so replace() doesn't copy them
new_obj._backend = self._backend
new_obj._backend_was_auto = self._backend_was_auto
return new_obj

def all_constraints_none(self) -> bool:
"""
Returns True if all structured-output constraint fields are None.
Expand Down