Skip to content

[Bugfix] Migrate to REGEX Library to prevent catastrophic backtracking #18454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 23, 2025
Merged
2 changes: 1 addition & 1 deletion .github/scripts/cleanup_pr_body.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sed -i '/\*\*BEFORE SUBMITTING, PLEASE READ.*\*\*/,$d' "${NEW}"

# Remove HTML <details> section that includes <summary> text of "PR Checklist (Click to Expand)"
python3 - <<EOF
import re
import regex as re
with open("${NEW}", "r") as file:
content = file.read()
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchmark_serving_structured_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def process_one_metric(
def evaluate(ret, args):
def _eval_correctness_json(expected, actual):
# extract json string from string using regex
import re
import regex as re

actual = actual.replace("\n", "").replace(" ", "").strip()
try:
Expand All @@ -687,7 +687,7 @@ def _eval_correctness_choice(expected, actual):
return actual in args.choice

def _eval_correctness_regex(expected, actual):
import re
import regex as re

return re.match(args.regex, actual) is not None

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/kernels/graph_machete_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import math
import pickle
import re
from collections import defaultdict

import matplotlib.pyplot as plt
import pandas as pd
import regex as re
import seaborn as sns
from torch.utils.benchmark import Measurement as TMeasurement

Expand Down
2 changes: 1 addition & 1 deletion examples/offline_inference/prithvi_geospatial_mae.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import argparse
import datetime
import os
import re
from typing import Union

import albumentations
import numpy as np
import rasterio
import regex as re
import torch
from einops import rearrange
from terratorch.datamodules import Sen1Floods11NonGeoDataModule
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ requires = [
"setuptools-scm>=8.0",
"torch == 2.7.0",
"wheel",
"regex",
"jinja2",
]
build-backend = "setuptools.build_meta"
Expand Down
1 change: 1 addition & 0 deletions requirements/build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ setuptools-scm>=8
torch==2.7.0
wheel
jinja2>=3.1.6
regex
1 change: 1 addition & 0 deletions requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
regex # Replace re for higher-performance regex matching
cachetools
psutil
sentencepiece # Required for LLaMA tokenizer.
Expand Down
2 changes: 1 addition & 1 deletion requirements/nightly_torch_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ matplotlib # required for qwen-vl test
# required for Multi-Modal Models Test (Standard)
num2words # required for smolvlm test
pqdm
timm # required for internvl test
timm # required for internvl test
3 changes: 1 addition & 2 deletions setup.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import json
import logging
import os
import re
import subprocess
import sys
from pathlib import Path
from shutil import which

import regex as re
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we revert this? This is painful to have and definitely not needed or a security vulnerability.

Revert PR: #18945

import torch
from packaging.version import Version, parse
from setuptools import Extension, setup
Expand Down Expand Up @@ -389,7 +389,6 @@ def run(self) -> None:
# vllm_flash_attn python code:
# Regex from
# `glob.translate('vllm/vllm_flash_attn/**/*.py', recursive=True)`
import re
compiled_regex = re.compile(
r"vllm/vllm_flash_attn/(?:[^/.][^/]*/)*(?!\.)[^/]*\.py")
file_members += list(
Expand Down
2 changes: 1 addition & 1 deletion tests/entrypoints/llm/test_guided_generate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-License-Identifier: Apache-2.0

import json
import re
import weakref
from enum import Enum

import jsonschema
import pytest
import regex as re
from pydantic import BaseModel

from vllm.distributed import cleanup_dist_env_and_memory
Expand Down
2 changes: 1 addition & 1 deletion tests/entrypoints/openai/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

# imports for guided decoding tests
import json
import re
from typing import Optional

import jsonschema
import openai # use the official client for correctness check
import pytest
import pytest_asyncio
import regex as re
import requests
import torch
from openai import BadRequestError, OpenAI
Expand Down
3 changes: 1 addition & 2 deletions tests/entrypoints/openai/test_completion.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# SPDX-License-Identifier: Apache-2.0

# imports for guided decoding tests
import json
import re
import shutil
from tempfile import TemporaryDirectory
from typing import Optional
Expand All @@ -11,6 +9,7 @@
import openai # use the official client for correctness check
import pytest
import pytest_asyncio
import regex as re
# downloading lora to test lora requests
from huggingface_hub import snapshot_download
from openai import BadRequestError
Expand Down
12 changes: 6 additions & 6 deletions tests/entrypoints/openai/test_prompt_validation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

# imports for guided decoding tests
import re

import openai
import pytest
import regex as re

from ...utils import RemoteOpenAIServer

Expand Down Expand Up @@ -32,7 +31,7 @@ async def test_out_of_vocab_token_ids():
client = remote_server.get_async_client()

with pytest.raises(openai.BadRequestError,
match=re.compile('.*out of vocabulary.*')):
match=re.compile('.*out of vocabulary.*').pattern):
await client.completions.create(model=model_name,
prompt=[999999],
max_tokens=5,
Expand All @@ -46,9 +45,10 @@ async def test_reject_multistep_with_guided_decoding():
with RemoteOpenAIServer(model_name, server_args) as remote_server:
client = remote_server.get_async_client()

with pytest.raises(openai.BadRequestError,
match=re.compile(
'.*Guided decoding .* multi-step decoding.*')):
with pytest.raises(
openai.BadRequestError,
match=re.compile(
'.*Guided decoding .* multi-step decoding.*').pattern):
await client.completions.create(
model=model_name,
prompt="Hello",
Expand Down
2 changes: 1 addition & 1 deletion tests/models/multimodal/generation/test_phi4mm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-License-Identifier: Apache-2.0

import os
import re
from collections.abc import Sequence
from typing import Optional

import librosa
import pytest
import regex as re
from huggingface_hub import snapshot_download
from transformers import AutoTokenizer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
for manipulating the input / output of HF & vLLM test runners, which are
typically specific to a small subset of models.
"""
import re
import types
from pathlib import PosixPath
from typing import Optional, Union

import regex as re
import torch
from PIL.Image import Image
from transformers import (AutoConfig, AutoTokenizer, BatchFeature,
Expand Down
4 changes: 2 additions & 2 deletions tests/tool_use/test_tool_choice_required.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# SPDX-License-Identifier: Apache-2.0
import json
import re
from copy import deepcopy
from unittest.mock import MagicMock

import pytest
import regex as re
from pydantic import TypeAdapter

from vllm.entrypoints.openai.protocol import (ChatCompletionRequest,
Expand Down Expand Up @@ -333,4 +333,4 @@ def test_streaming_output_valid(output, empty_params, delta_len):
combined_messages += message.tool_calls[0].function.arguments
combined_messages += "}]"
assert json.loads(combined_messages) == output
assert json.dumps(json.loads(combined_messages)) == output_json
assert json.dumps(json.loads(combined_messages)) == output_json
2 changes: 1 addition & 1 deletion tests/v1/entrypoints/llm/test_struct_output_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from __future__ import annotations

import json
import re
from enum import Enum
from typing import TYPE_CHECKING, Any

import jsonschema
import pytest
import regex as re
from pydantic import BaseModel

from tests.reasoning.utils import run_reasoning_extraction
Expand Down
2 changes: 1 addition & 1 deletion tests/v1/entrypoints/openai/test_completion.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SPDX-License-Identifier: Apache-2.0

import re
from typing import Optional

import openai # use the official client for correctness check
import pytest
import pytest_asyncio
import regex as re
from openai import BadRequestError

from tests.utils import RemoteOpenAIServer
Expand Down
3 changes: 2 additions & 1 deletion tests/v1/sample/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# SPDX-License-Identifier: Apache-2.0

import re
from enum import Enum
from typing import Optional

import regex as re

from vllm import CompletionOutput


Expand Down
2 changes: 1 addition & 1 deletion vllm/collect_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,4 @@ def main():


if __name__ == '__main__':
main()
main()
2 changes: 1 addition & 1 deletion vllm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import hashlib
import inspect
import json
import re
import textwrap
import uuid
import warnings
Expand All @@ -20,6 +19,7 @@
from typing import (TYPE_CHECKING, Any, Callable, ClassVar, Literal, Optional,
Protocol, TypeVar, Union, cast, get_args, get_origin)

import regex as re
import torch
from torch.distributed import ProcessGroup, ReduceOp
from transformers import PretrainedConfig
Expand Down
2 changes: 1 addition & 1 deletion vllm/engine/arg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import argparse
import dataclasses
import json
import re
import sys
import threading
import warnings
Expand All @@ -13,6 +12,7 @@
from typing import (Annotated, Any, Callable, Dict, List, Literal, Optional,
Type, TypeVar, Union, cast, get_args, get_origin)

import regex as re
import torch
from typing_extensions import TypeIs, deprecated

Expand Down
2 changes: 1 addition & 1 deletion vllm/entrypoints/openai/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import inspect
import multiprocessing
import os
import re
import signal
import socket
import tempfile
Expand All @@ -21,6 +20,7 @@
from typing import Annotated, Optional, Union

import prometheus_client
import regex as re
import uvloop
from fastapi import APIRouter, Depends, FastAPI, Form, HTTPException, Request
from fastapi.exceptions import RequestValidationError
Expand Down
2 changes: 1 addition & 1 deletion vllm/entrypoints/openai/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# Adapted from
# https://github.com/lm-sys/FastChat/blob/168ccc29d3f7edc50823016105c024fe2282732a/fastchat/protocol/openai_api_protocol.py
import json
import re
import time
from http import HTTPStatus
from typing import Annotated, Any, ClassVar, Literal, Optional, Union

import regex as re
import torch
from fastapi import HTTPException, UploadFile
from pydantic import (BaseModel, ConfigDict, Field, TypeAdapter,
Expand Down
2 changes: 1 addition & 1 deletion vllm/entrypoints/openai/serving_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import asyncio
import json
import re
import time
from collections.abc import AsyncGenerator, AsyncIterator
from collections.abc import Sequence as GenericSequence
from typing import Callable, Final, Optional, Union

import jinja2
import partial_json_parser
import regex as re
from fastapi import Request
from pydantic import TypeAdapter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# SPDX-License-Identifier: Apache-2.0

import re
from collections.abc import Sequence
from typing import Union

import regex as re

from vllm.entrypoints.openai.protocol import (ChatCompletionRequest,
DeltaFunctionCall, DeltaMessage,
DeltaToolCall,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-License-Identifier: Apache-2.0

import json
import re
from collections.abc import Sequence
from json import JSONDecoder
from typing import Union

import partial_json_parser
import regex as re
from partial_json_parser.core.options import Allow

from vllm.entrypoints.chat_utils import random_tool_call_id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SPDX-License-Identifier: Apache-2.0

import json
import re
from collections.abc import Sequence
from typing import Union

import partial_json_parser
import regex as re
from partial_json_parser.core.options import Allow

from vllm.entrypoints.chat_utils import random_tool_call_id
Expand Down
Loading