Structured Outputs For Reasoning Models#
When working with reasoning models that use special tokens like <think>...</think>
to denote reasoning sections, you might want to allow free-form text within these sections while still enforcing grammar constraints on the rest of the output.
SGLang provides a feature to disable grammar restrictions within reasoning sections. This is particularly useful for models that need to perform complex reasoning steps before providing a structured output.
To enable this feature, use the --reasoning-parser
flag which decide the think_end_token, such as </think>
, when launching the server. You can also specify the reasoning parser using the --reasoning-parser
flag.
Supported Models#
Currently, SGLang supports the following reasoning models:
DeepSeek R1 series: The reasoning content is wrapped with
<think>
and</think>
tags.QwQ: The reasoning content is wrapped with
<think>
and</think>
tags.
Usage#
OpenAI Compatible API#
Specify the --grammar-backend
, --reasoning-parser
option.
[1]:
import openai
import os
from sglang.test.test_utils import is_in_ci
if is_in_ci():
from patch import launch_server_cmd
else:
from sglang.utils import launch_server_cmd
from sglang.utils import wait_for_server, print_highlight, terminate_process
os.environ["TOKENIZERS_PARALLELISM"] = "false"
server_process, port = launch_server_cmd(
"python -m sglang.launch_server --model-path deepseek-ai/DeepSeek-R1-Distill-Qwen-7B --host 0.0.0.0 --reasoning-parser deepseek-r1"
)
wait_for_server(f"http://localhost:{port}")
client = openai.Client(base_url=f"http://127.0.0.1:{port}/v1", api_key="None")
[2025-07-14 10:04:52] server_args=ServerArgs(model_path='deepseek-ai/DeepSeek-R1-Distill-Qwen-7B', tokenizer_path='deepseek-ai/DeepSeek-R1-Distill-Qwen-7B', tokenizer_mode='auto', skip_tokenizer_init=False, skip_server_warmup=False, load_format='auto', model_loader_extra_config='{}', trust_remote_code=False, dtype='auto', kv_cache_dtype='auto', quantization=None, quantization_param_path=None, context_length=None, device='cuda', served_model_name='deepseek-ai/DeepSeek-R1-Distill-Qwen-7B', chat_template=None, completion_template=None, is_embedding=False, enable_multimodal=None, revision=None, hybrid_kvcache_ratio=None, swa_full_tokens_ratio=0.8, impl='auto', host='0.0.0.0', port=33217, nccl_port=None, mem_fraction_static=0.874, max_running_requests=200, max_total_tokens=20480, chunked_prefill_size=8192, max_prefill_tokens=16384, schedule_policy='fcfs', schedule_conservativeness=1.0, cpu_offload_gb=0, page_size=1, tp_size=1, pp_size=1, max_micro_batch_size=None, stream_interval=1, stream_output=False, random_seed=329841484, constrained_json_whitespace_pattern=None, watchdog_timeout=300, dist_timeout=None, download_dir=None, base_gpu_id=0, gpu_id_step=1, sleep_on_idle=False, log_level='info', log_level_http=None, log_requests=False, log_requests_level=0, crash_dump_folder=None, show_time_cost=False, enable_metrics=False, bucket_time_to_first_token=None, bucket_e2e_request_latency=None, bucket_inter_token_latency=None, collect_tokens_histogram=False, decode_log_interval=40, enable_request_time_stats_logging=False, kv_events_config=None, api_key=None, file_storage_path='sglang_storage', enable_cache_report=False, reasoning_parser='deepseek-r1', tool_call_parser=None, dp_size=1, load_balance_method='round_robin', dist_init_addr=None, nnodes=1, node_rank=0, json_model_override_args='{}', preferred_sampling_params=None, max_lora_rank=None, lora_target_modules=None, lora_paths=None, max_loras_per_batch=8, lora_backend='triton', attention_backend=None, sampling_backend='flashinfer', grammar_backend='xgrammar', mm_attention_backend=None, speculative_algorithm=None, speculative_draft_model_path=None, speculative_num_steps=None, speculative_eagle_topk=None, speculative_num_draft_tokens=None, speculative_accept_threshold_single=1.0, speculative_accept_threshold_acc=1.0, speculative_token_map=None, ep_size=1, enable_ep_moe=False, enable_deepep_moe=False, enable_flashinfer_moe=False, enable_flashinfer_allreduce_fusion=False, deepep_mode='auto', ep_num_redundant_experts=0, ep_dispatch_algorithm='static', init_expert_location='trivial', enable_eplb=False, eplb_algorithm='auto', eplb_rebalance_num_iterations=1000, eplb_rebalance_layers_per_chunk=None, expert_distribution_recorder_mode=None, expert_distribution_recorder_buffer_size=1000, enable_expert_distribution_metrics=False, deepep_config=None, moe_dense_tp_size=None, enable_double_sparsity=False, ds_channel_config_path=None, ds_heavy_channel_num=32, ds_heavy_token_num=256, ds_heavy_channel_type='qk', ds_sparse_decode_threshold=4096, disable_radix_cache=False, cuda_graph_max_bs=None, cuda_graph_bs=None, disable_cuda_graph=True, disable_cuda_graph_padding=False, enable_profile_cuda_graph=False, enable_nccl_nvls=False, enable_tokenizer_batch_encode=False, disable_outlines_disk_cache=False, disable_custom_all_reduce=False, enable_mscclpp=False, disable_overlap_schedule=False, disable_overlap_cg_plan=False, enable_mixed_chunk=False, enable_dp_attention=False, enable_dp_lm_head=False, enable_two_batch_overlap=False, enable_torch_compile=False, torch_compile_max_bs=32, torchao_config='', enable_nan_detection=False, enable_p2p_check=False, triton_attention_reduce_in_fp32=False, triton_attention_num_kv_splits=8, num_continuous_decode_steps=1, delete_ckpt_after_loading=False, enable_memory_saver=False, allow_auto_truncate=False, enable_custom_logit_processor=False, enable_hierarchical_cache=False, hicache_ratio=2.0, hicache_size=0, hicache_write_policy='write_through_selective', hicache_io_backend='', flashinfer_mla_disable_ragged=False, disable_shared_experts_fusion=False, disable_chunked_prefix_cache=False, disable_fast_image_processor=False, enable_return_hidden_states=False, enable_triton_kernel_moe=False, warmups=None, disable_hybrid_swa_memory=False, debug_tensor_dump_output_folder=None, debug_tensor_dump_input_file=None, debug_tensor_dump_inject=False, debug_tensor_dump_prefill_only=False, disaggregation_mode='null', disaggregation_transfer_backend='mooncake', disaggregation_bootstrap_port=8998, disaggregation_decode_tp=None, disaggregation_decode_dp=None, disaggregation_prefill_pp=1, disaggregation_ib_device=None, num_reserved_decode_tokens=512, pdlb_url=None, custom_weight_loader=[], weight_loader_disable_mmap=False)
[2025-07-14 10:05:04] Attention backend not set. Use fa3 backend by default.
[2025-07-14 10:05:04] Init torch distributed begin.
[2025-07-14 10:05:04] Init torch distributed ends. mem usage=0.00 GB
[2025-07-14 10:05:05] Load weight begin. avail mem=53.54 GB
[2025-07-14 10:05:05] The weight of LmHead is not packed
[2025-07-14 10:05:05] Using model weights format ['*.safetensors']
Loading safetensors checkpoint shards: 0% Completed | 0/2 [00:00<?, ?it/s]
Loading safetensors checkpoint shards: 50% Completed | 1/2 [00:01<00:01, 1.36s/it]
Loading safetensors checkpoint shards: 100% Completed | 2/2 [00:02<00:00, 1.27s/it]
Loading safetensors checkpoint shards: 100% Completed | 2/2 [00:02<00:00, 1.29s/it]
[2025-07-14 10:05:08] Load weight end. type=Qwen2ForCausalLM, dtype=torch.bfloat16, avail mem=39.19 GB, mem usage=14.35 GB.
[2025-07-14 10:05:08] KV Cache is allocated. #tokens: 20480, K size: 0.55 GB, V size: 0.55 GB
[2025-07-14 10:05:08] Memory pool end. avail mem=37.82 GB
[2025-07-14 10:05:09] max_total_num_tokens=20480, chunked_prefill_size=8192, max_prefill_tokens=16384, max_running_requests=200, context_len=131072, available_gpu_mem=37.72 GB
[2025-07-14 10:05:09] INFO: Started server process [2365148]
[2025-07-14 10:05:09] INFO: Waiting for application startup.
[2025-07-14 10:05:09] INFO: Application startup complete.
[2025-07-14 10:05:09] INFO: Uvicorn running on http://0.0.0.0:33217 (Press CTRL+C to quit)
[2025-07-14 10:05:10] INFO: 127.0.0.1:36908 - "GET /v1/models HTTP/1.1" 200 OK
[2025-07-14 10:05:10] INFO: 127.0.0.1:36916 - "GET /get_model_info HTTP/1.1" 200 OK
[2025-07-14 10:05:10] Prefill batch. #new-seq: 1, #new-token: 7, #cached-token: 0, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:10.760016
[2025-07-14 10:05:11] INFO: 127.0.0.1:36922 - "POST /generate HTTP/1.1" 200 OK
[2025-07-14 10:05:11] The server is fired up and ready to roll!
NOTE: Typically, the server runs in a separate terminal.
In this notebook, we run the server and notebook code together, so their outputs are combined.
To improve clarity, the server logs are displayed in the original black color, while the notebook outputs are highlighted in blue.
We are running those notebooks in a CI parallel environment, so the throughput is not representative of the actual performance.
JSON#
you can directly define a JSON schema or use Pydantic to define and validate the response.
Using Pydantic
[2]:
from pydantic import BaseModel, Field
# Define the schema using Pydantic
class CapitalInfo(BaseModel):
name: str = Field(..., pattern=r"^\w+$", description="Name of the capital city")
population: int = Field(..., description="Population of the capital city")
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
messages=[
{
"role": "assistant",
"content": "Give me the information and population of the capital of France in the JSON format.",
},
],
temperature=0,
max_tokens=2048,
response_format={
"type": "json_schema",
"json_schema": {
"name": "foo",
# convert the pydantic model to json schema
"schema": CapitalInfo.model_json_schema(),
},
},
)
print_highlight(
f"reasoing_content: {response.choices[0].message.reasoning_content}\n\ncontent: {response.choices[0].message.content}"
)
[2025-07-14 10:05:15] Prefill batch. #new-seq: 1, #new-token: 21, #cached-token: 1, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:15.380982
[2025-07-14 10:05:16] Decode batch. #running-req: 1, #token: 55, token usage: 0.00, cuda graph: False, gen throughput (token/s): 5.61, #queue-req: 0, timestamp: 2025-07-14T10:05:16.388292
[2025-07-14 10:05:16] Decode batch. #running-req: 1, #token: 95, token usage: 0.00, cuda graph: False, gen throughput (token/s): 110.66, #queue-req: 0, timestamp: 2025-07-14T10:05:16.749750
[2025-07-14 10:05:17] Decode batch. #running-req: 1, #token: 135, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.79, #queue-req: 0, timestamp: 2025-07-14T10:05:17.110783
[2025-07-14 10:05:17] Decode batch. #running-req: 1, #token: 175, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.62, #queue-req: 0, timestamp: 2025-07-14T10:05:17.472378
[2025-07-14 10:05:17] Decode batch. #running-req: 1, #token: 215, token usage: 0.01, cuda graph: False, gen throughput (token/s): 111.15, #queue-req: 0, timestamp: 2025-07-14T10:05:17.832235
[2025-07-14 10:05:18] Decode batch. #running-req: 1, #token: 255, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.98, #queue-req: 0, timestamp: 2025-07-14T10:05:18.192656
[2025-07-14 10:05:18] Decode batch. #running-req: 1, #token: 295, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.64, #queue-req: 0, timestamp: 2025-07-14T10:05:18.554177
[2025-07-14 10:05:18] Decode batch. #running-req: 1, #token: 335, token usage: 0.02, cuda graph: False, gen throughput (token/s): 108.60, #queue-req: 0, timestamp: 2025-07-14T10:05:18.922505
[2025-07-14 10:05:19] Decode batch. #running-req: 1, #token: 375, token usage: 0.02, cuda graph: False, gen throughput (token/s): 110.68, #queue-req: 0, timestamp: 2025-07-14T10:05:19.283894
[2025-07-14 10:05:19] Decode batch. #running-req: 1, #token: 415, token usage: 0.02, cuda graph: False, gen throughput (token/s): 110.60, #queue-req: 0, timestamp: 2025-07-14T10:05:19.645576
[2025-07-14 10:05:19] INFO: 127.0.0.1:36932 - "POST /v1/chat/completions HTTP/1.1" 200 OK
Next, I considered the population. I remember that Paris has a large population, but I'm not exactly sure of the current number. I think it's around 2 million, but I'm not 100% certain. I should double-check that to make sure I provide accurate information.
I also need to structure this in JSON. JSON requires key-value pairs, so I'll need to define the keys appropriately. Maybe "city" for the name, "country" for the capital, and "population" for the number. I should make sure the syntax is correct, with proper commas and quotation marks.
Wait, I should also think about the format. The user wants it in JSON, so I'll present it as a JSON object. I'll make sure there are no typos and that the data is correctly formatted. Maybe I'll write it out step by step to avoid mistakes.
Another thing to consider is whether the population figure is up to date. Since I'm not accessing real-time data, I'll go with the most recent estimate I have. I recall that Paris has grown a bit in recent years, so 2 million seems reasonable, but I should confirm if it's 2.1 million or something else.
I also wonder if the user needs more details, like the area or the establishment year of the city, but the query specifically mentions population, so I'll stick to that. Still, it's good to know that I can provide additional information if needed.
Finally, I'll present the JSON in a clear and concise manner, making sure it's easy for the user to understand and use. I'll review the JSON structure to ensure there are no syntax errors before sending it back to the user.
content: {"name": "Paris", "population": 2145000}
JSON Schema Directly
[3]:
import json
json_schema = json.dumps(
{
"type": "object",
"properties": {
"name": {"type": "string", "pattern": "^[\\w]+$"},
"population": {"type": "integer"},
},
"required": ["name", "population"],
}
)
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
messages=[
{
"role": "assistant",
"content": "Give me the information and population of the capital of France in the JSON format.",
},
],
temperature=0,
max_tokens=2048,
response_format={
"type": "json_schema",
"json_schema": {"name": "foo", "schema": json.loads(json_schema)},
},
)
print_highlight(
f"reasoing_content: {response.choices[0].message.reasoning_content}\n\ncontent: {response.choices[0].message.content}"
)
[2025-07-14 10:05:19] Prefill batch. #new-seq: 1, #new-token: 1, #cached-token: 21, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:19.927616
[2025-07-14 10:05:20] Decode batch. #running-req: 1, #token: 34, token usage: 0.00, cuda graph: False, gen throughput (token/s): 99.83, #queue-req: 0, timestamp: 2025-07-14T10:05:20.046241
[2025-07-14 10:05:20] Decode batch. #running-req: 1, #token: 74, token usage: 0.00, cuda graph: False, gen throughput (token/s): 110.76, #queue-req: 0, timestamp: 2025-07-14T10:05:20.407369
[2025-07-14 10:05:20] Decode batch. #running-req: 1, #token: 114, token usage: 0.01, cuda graph: False, gen throughput (token/s): 111.57, #queue-req: 0, timestamp: 2025-07-14T10:05:20.765897
[2025-07-14 10:05:21] Decode batch. #running-req: 1, #token: 154, token usage: 0.01, cuda graph: False, gen throughput (token/s): 111.71, #queue-req: 0, timestamp: 2025-07-14T10:05:21.123974
[2025-07-14 10:05:21] Decode batch. #running-req: 1, #token: 194, token usage: 0.01, cuda graph: False, gen throughput (token/s): 111.47, #queue-req: 0, timestamp: 2025-07-14T10:05:21.482827
[2025-07-14 10:05:21] Decode batch. #running-req: 1, #token: 234, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.68, #queue-req: 0, timestamp: 2025-07-14T10:05:21.844225
[2025-07-14 10:05:22] Decode batch. #running-req: 1, #token: 274, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.58, #queue-req: 0, timestamp: 2025-07-14T10:05:22.205951
[2025-07-14 10:05:22] Decode batch. #running-req: 1, #token: 314, token usage: 0.02, cuda graph: False, gen throughput (token/s): 109.89, #queue-req: 0, timestamp: 2025-07-14T10:05:22.569947
[2025-07-14 10:05:22] INFO: 127.0.0.1:36932 - "POST /v1/chat/completions HTTP/1.1" 200 OK
Next, I thought about the structure. The user wants JSON, so I need to format it correctly with keys like "city", "population", and maybe "country". I should make sure the syntax is correct—no typos, proper commas, and brackets.
I also considered the user's possible needs. They might be doing a project or a presentation, so providing accurate data is crucial. Maybe they're a student learning about France's capitals or someone compiling demographic data. Either way, precision is key.
I decided to present the information clearly, ensuring the JSON is valid and easy to read. I included the population as 3.617 million, which I believe is the most recent figure I could recall. I also added a comment to explain the units, just in case the user wasn't sure.
Finally, I made sure to offer further help in case they needed more details or adjustments. That way, the response is helpful and user-friendly.
content: {
"name": "Paris",
"population": 3617000
}
EBNF#
[4]:
ebnf_grammar = """
root ::= city | description
city ::= "London" | "Paris" | "Berlin" | "Rome"
description ::= city " is " status
status ::= "the capital of " country
country ::= "England" | "France" | "Germany" | "Italy"
"""
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
messages=[
{"role": "system", "content": "You are a helpful geography bot."},
{
"role": "assistant",
"content": "Give me the information and population of the capital of France in the JSON format.",
},
],
temperature=0,
max_tokens=2048,
extra_body={"ebnf": ebnf_grammar},
)
print_highlight(
f"reasoing_content: {response.choices[0].message.reasoning_content}\n\ncontent: {response.choices[0].message.content}"
)
[2025-07-14 10:05:22] Prefill batch. #new-seq: 1, #new-token: 28, #cached-token: 1, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:22.726652
[2025-07-14 10:05:22] Decode batch. #running-req: 1, #token: 53, token usage: 0.00, cuda graph: False, gen throughput (token/s): 100.71, #queue-req: 0, timestamp: 2025-07-14T10:05:22.967130
[2025-07-14 10:05:23] Decode batch. #running-req: 1, #token: 93, token usage: 0.00, cuda graph: False, gen throughput (token/s): 109.75, #queue-req: 0, timestamp: 2025-07-14T10:05:23.331607
[2025-07-14 10:05:23] Decode batch. #running-req: 1, #token: 133, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.57, #queue-req: 0, timestamp: 2025-07-14T10:05:23.693364
[2025-07-14 10:05:24] Decode batch. #running-req: 1, #token: 173, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.67, #queue-req: 0, timestamp: 2025-07-14T10:05:24.054812
[2025-07-14 10:05:24] Decode batch. #running-req: 1, #token: 213, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.28, #queue-req: 0, timestamp: 2025-07-14T10:05:24.417519
[2025-07-14 10:05:24] Decode batch. #running-req: 1, #token: 253, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.72, #queue-req: 0, timestamp: 2025-07-14T10:05:24.778795
[2025-07-14 10:05:25] Decode batch. #running-req: 1, #token: 293, token usage: 0.01, cuda graph: False, gen throughput (token/s): 109.63, #queue-req: 0, timestamp: 2025-07-14T10:05:25.143651
[2025-07-14 10:05:25] Decode batch. #running-req: 1, #token: 333, token usage: 0.02, cuda graph: False, gen throughput (token/s): 108.98, #queue-req: 0, timestamp: 2025-07-14T10:05:25.510692
[2025-07-14 10:05:25] Decode batch. #running-req: 1, #token: 373, token usage: 0.02, cuda graph: False, gen throughput (token/s): 110.49, #queue-req: 0, timestamp: 2025-07-14T10:05:25.872725
[2025-07-14 10:05:26] Decode batch. #running-req: 1, #token: 413, token usage: 0.02, cuda graph: False, gen throughput (token/s): 109.45, #queue-req: 0, timestamp: 2025-07-14T10:05:26.238195
[2025-07-14 10:05:26] Decode batch. #running-req: 1, #token: 453, token usage: 0.02, cuda graph: False, gen throughput (token/s): 110.35, #queue-req: 0, timestamp: 2025-07-14T10:05:26.600683
[2025-07-14 10:05:26] Decode batch. #running-req: 1, #token: 493, token usage: 0.02, cuda graph: False, gen throughput (token/s): 110.67, #queue-req: 0, timestamp: 2025-07-14T10:05:26.962117
[2025-07-14 10:05:27] Decode batch. #running-req: 1, #token: 533, token usage: 0.03, cuda graph: False, gen throughput (token/s): 106.98, #queue-req: 0, timestamp: 2025-07-14T10:05:27.336005
[2025-07-14 10:05:27] Decode batch. #running-req: 1, #token: 573, token usage: 0.03, cuda graph: False, gen throughput (token/s): 107.74, #queue-req: 0, timestamp: 2025-07-14T10:05:27.707256
[2025-07-14 10:05:28] Decode batch. #running-req: 1, #token: 613, token usage: 0.03, cuda graph: False, gen throughput (token/s): 108.15, #queue-req: 0, timestamp: 2025-07-14T10:05:28.077106
[2025-07-14 10:05:28] INFO: 127.0.0.1:36932 - "POST /v1/chat/completions HTTP/1.1" 200 OK
First, I need to figure out what the user is really looking for. They might be creating a dataset or a project that requires the capitals of various nations. Maybe they're a student working on a geography assignment or a developer building a mapping application. Either way, they need accurate and reliable data.
Looking at the list they provided: Albania, Algeria, Australia, Austria, Brazil, Canada, China, Colombia, Denmark, Egypt, France, Germany, Greece, Hungary, Iceland, India, Indonesia, Ireland, Italy, Japan, Mexico, Netherlands, Nigeria, Poland, Portugal, Russia, Saudi Arabia, Spain, Sweden, Switzerland, Thailand, Turkey, UK, USA. That's quite a comprehensive list, covering multiple continents.
I should make sure each country's capital is correct. I know some capitals off the top of my head, like Albania's Tirana, Algeria's Algiers, Australia's Canberra. But I should double-check the rest to avoid mistakes. For example, I'm pretty sure Germany's capital is Berlin, but I should confirm that. Same with countries like Mexico and Spain, their capitals are Mexico City and Madrid, respectively.
The user wants the information in JSON format, so each country will have a key with its name and capital, along with the population. I'll need to look up the most recent population estimates for each capital. Population numbers can change, so it's important to use the latest data. For example, as of 2023, Paris has a population around 2.1 million, but it's growing, so maybe I should note that the figure is approximate.
I should structure the JSON array correctly, ensuring each object has the same fields. Also, I'll add a comment at the top to explain the data, making it clear for anyone reading the JSON.
I need to be careful with the syntax to avoid errors. JSON requires proper quotation marks and commas. Each object in the array should be separated by a comma, and the entire structure should be valid. Maybe I'll write it out step by step to ensure accuracy.
Additionally, I should consider if the user needs more details, like the country's area or other statistics, but since they only asked for population, I'll stick to that. However, offering to include more data in the future might be a good idea, showing flexibility.
Lastly, I'll make sure the JSON is well-formatted and easy to read, perhaps by indenting it for better readability. That way, the user can easily parse the data without issues.
content: London is the capital of France
Regular expression#
[5]:
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
messages=[
{"role": "assistant", "content": "What is the capital of France?"},
],
temperature=0,
max_tokens=2048,
extra_body={"regex": "(Paris|London)"},
)
print_highlight(
f"reasoing_content: {response.choices[0].message.reasoning_content}\n\ncontent: {response.choices[0].message.content}"
)
[2025-07-14 10:05:28] Prefill batch. #new-seq: 1, #new-token: 11, #cached-token: 2, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:28.207692
[2025-07-14 10:05:28] Decode batch. #running-req: 1, #token: 40, token usage: 0.00, cuda graph: False, gen throughput (token/s): 101.58, #queue-req: 0, timestamp: 2025-07-14T10:05:28.470867
[2025-07-14 10:05:28] Decode batch. #running-req: 1, #token: 80, token usage: 0.00, cuda graph: False, gen throughput (token/s): 112.06, #queue-req: 0, timestamp: 2025-07-14T10:05:28.827829
[2025-07-14 10:05:29] Decode batch. #running-req: 1, #token: 120, token usage: 0.01, cuda graph: False, gen throughput (token/s): 111.60, #queue-req: 0, timestamp: 2025-07-14T10:05:29.186254
[2025-07-14 10:05:29] INFO: 127.0.0.1:36932 - "POST /v1/chat/completions HTTP/1.1" 200 OK
content: Paris
Structural Tag#
[6]:
tool_get_current_weather = {
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to find the weather for, e.g. 'San Francisco'",
},
"state": {
"type": "string",
"description": "the two-letter abbreviation for the state that the city is"
" in, e.g. 'CA' which would mean 'California'",
},
"unit": {
"type": "string",
"description": "The unit to fetch the temperature in",
"enum": ["celsius", "fahrenheit"],
},
},
"required": ["city", "state", "unit"],
},
},
}
tool_get_current_date = {
"type": "function",
"function": {
"name": "get_current_date",
"description": "Get the current date and time for a given timezone",
"parameters": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "The timezone to fetch the current date and time for, e.g. 'America/New_York'",
}
},
"required": ["timezone"],
},
},
}
schema_get_current_weather = tool_get_current_weather["function"]["parameters"]
schema_get_current_date = tool_get_current_date["function"]["parameters"]
def get_messages():
return [
{
"role": "system",
"content": f"""
# Tool Instructions
- Always execute python code in messages that you share.
- When looking for real time information use relevant functions if available else fallback to brave_search
You have access to the following functions:
Use the function 'get_current_weather' to: Get the current weather in a given location
{tool_get_current_weather["function"]}
Use the function 'get_current_date' to: Get the current date and time for a given timezone
{tool_get_current_date["function"]}
If a you choose to call a function ONLY reply in the following format:
<{{start_tag}}={{function_name}}>{{parameters}}{{end_tag}}
where
start_tag => `<function`
parameters => a JSON dict with the function argument name as key and function argument value as value.
end_tag => `</function>`
Here is an example,
<function=example_function_name>{{"example_name": "example_value"}}</function>
Reminder:
- Function calls MUST follow the specified format
- Required parameters MUST be specified
- Only call one function at a time
- Put the entire function call reply on one line
- Always add your sources when using search results to answer the user query
You are a helpful assistant.""",
},
{
"role": "assistant",
"content": "You are in New York. Please get the current date and time, and the weather.",
},
]
messages = get_messages()
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
messages=messages,
response_format={
"type": "structural_tag",
"max_new_tokens": 2048,
"structures": [
{
"begin": "<function=get_current_weather>",
"schema": schema_get_current_weather,
"end": "</function>",
},
{
"begin": "<function=get_current_date>",
"schema": schema_get_current_date,
"end": "</function>",
},
],
"triggers": ["<function="],
},
)
print_highlight(
f"reasoing_content: {response.choices[0].message.reasoning_content}\n\ncontent: {response.choices[0].message.content}"
)
[2025-07-14 10:05:30] Prefill batch. #new-seq: 1, #new-token: 472, #cached-token: 1, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:30.260818
[2025-07-14 10:05:30] Decode batch. #running-req: 1, #token: 500, token usage: 0.02, cuda graph: False, gen throughput (token/s): 29.41, #queue-req: 0, timestamp: 2025-07-14T10:05:30.546345
[2025-07-14 10:05:30] Decode batch. #running-req: 1, #token: 540, token usage: 0.03, cuda graph: False, gen throughput (token/s): 109.61, #queue-req: 0, timestamp: 2025-07-14T10:05:30.911275
[2025-07-14 10:05:31] Decode batch. #running-req: 1, #token: 580, token usage: 0.03, cuda graph: False, gen throughput (token/s): 108.78, #queue-req: 0, timestamp: 2025-07-14T10:05:31.278997
[2025-07-14 10:05:31] Decode batch. #running-req: 1, #token: 620, token usage: 0.03, cuda graph: False, gen throughput (token/s): 108.26, #queue-req: 0, timestamp: 2025-07-14T10:05:31.648485
[2025-07-14 10:05:32] Decode batch. #running-req: 1, #token: 660, token usage: 0.03, cuda graph: False, gen throughput (token/s): 103.48, #queue-req: 0, timestamp: 2025-07-14T10:05:32.035013
[2025-07-14 10:05:32] Decode batch. #running-req: 1, #token: 700, token usage: 0.03, cuda graph: False, gen throughput (token/s): 104.38, #queue-req: 0, timestamp: 2025-07-14T10:05:32.418232
[2025-07-14 10:05:32] Decode batch. #running-req: 1, #token: 740, token usage: 0.04, cuda graph: False, gen throughput (token/s): 107.98, #queue-req: 0, timestamp: 2025-07-14T10:05:32.788666
[2025-07-14 10:05:33] Decode batch. #running-req: 1, #token: 780, token usage: 0.04, cuda graph: False, gen throughput (token/s): 105.20, #queue-req: 0, timestamp: 2025-07-14T10:05:33.168882
[2025-07-14 10:05:33] Decode batch. #running-req: 1, #token: 820, token usage: 0.04, cuda graph: False, gen throughput (token/s): 108.58, #queue-req: 0, timestamp: 2025-07-14T10:05:33.537258
[2025-07-14 10:05:33] Decode batch. #running-req: 1, #token: 860, token usage: 0.04, cuda graph: False, gen throughput (token/s): 108.53, #queue-req: 0, timestamp: 2025-07-14T10:05:33.905822
[2025-07-14 10:05:34] Decode batch. #running-req: 1, #token: 900, token usage: 0.04, cuda graph: False, gen throughput (token/s): 107.08, #queue-req: 0, timestamp: 2025-07-14T10:05:34.279388
[2025-07-14 10:05:34] Decode batch. #running-req: 1, #token: 940, token usage: 0.05, cuda graph: False, gen throughput (token/s): 101.88, #queue-req: 0, timestamp: 2025-07-14T10:05:34.672029
[2025-07-14 10:05:35] Decode batch. #running-req: 1, #token: 980, token usage: 0.05, cuda graph: False, gen throughput (token/s): 99.29, #queue-req: 0, timestamp: 2025-07-14T10:05:35.074893
[2025-07-14 10:05:35] INFO: 127.0.0.1:36932 - "POST /v1/chat/completions HTTP/1.1" 200 OK
First, I remember that the user asked to get the current date and time for a specific timezone. So I should use the 'get_current_date' function. The function requires a 'timezone' parameter, which in this case is 'America/New_York'. I'll need to structure the function call correctly, making sure to include the timezone argument in the JSON parameters.
Next, for the weather, the user mentioned they're in New York, so I need to get the current weather for that location. I should use the 'get_current_weather' function. This function takes a 'city' and 'state' parameter. I know that New York is in the state of NY, so I'll set 'city' as 'New York' and 'state' as 'NY'. The unit isn't specified, but since the user didn't mention it, maybe I should leave it out or choose a default. Wait, looking back at the function's parameters, I see that 'unit' is optional. So I can omit it if not specified.
Putting it all together, I'll need to call both functions. First, 'get_current_date' with the timezone parameter. Then, 'get_current_weather' with city and state parameters. I should make sure each function call is separate and properly formatted according to the instructions, using the correct syntax for JSON parameters.
Wait, but should I include the unit in the weather function? The function allows 'celsius' or 'fahrenheit', but since the user didn't specify, maybe it's safer to leave it out. Alternatively, I could include it as optional, but perhaps the function can infer the unit from the location. Hmm, I think I'll proceed without specifying the unit to keep it simple.
Also, I need to ensure that each function call is on its own line and in the correct format. So first, I'll call get_current_date with the timezone, then get_current_weather with city and state. I should double-check the syntax to make sure there are no errors, like missing quotes or incorrect key names.
I think that's all. I'll structure the function calls accordingly, making sure each is properly formatted and includes all necessary parameters.
content:
Native API and SGLang Runtime (SRT)#
JSON#
Using Pydantic
[7]:
import requests
from pydantic import BaseModel, Field
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-7B")
# Define the schema using Pydantic
class CapitalInfo(BaseModel):
name: str = Field(..., pattern=r"^\w+$", description="Name of the capital city")
population: int = Field(..., description="Population of the capital city")
messages = [
{
"role": "assistant",
"content": "Give me the information and population of the capital of France in the JSON format.",
},
]
text = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
# Make API request
response = requests.post(
f"http://localhost:{port}/generate",
json={
"text": text,
"sampling_params": {
"temperature": 0,
"max_new_tokens": 2048,
"json_schema": json.dumps(CapitalInfo.model_json_schema()),
},
},
)
print(response.json())
reasoing_content = response.json()["text"].split("</think>")[0]
content = response.json()["text"].split("</think>")[1]
print_highlight(f"reasoing_content: {reasoing_content}\n\ncontent: {content}")
[2025-07-14 10:05:39] Prefill batch. #new-seq: 1, #new-token: 22, #cached-token: 1, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:39.465049
[2025-07-14 10:05:39] Decode batch. #running-req: 1, #token: 46, token usage: 0.00, cuda graph: False, gen throughput (token/s): 8.66, #queue-req: 0, timestamp: 2025-07-14T10:05:39.695975
[2025-07-14 10:05:40] Decode batch. #running-req: 1, #token: 86, token usage: 0.00, cuda graph: False, gen throughput (token/s): 105.65, #queue-req: 0, timestamp: 2025-07-14T10:05:40.074595
[2025-07-14 10:05:40] Decode batch. #running-req: 1, #token: 126, token usage: 0.01, cuda graph: False, gen throughput (token/s): 107.95, #queue-req: 0, timestamp: 2025-07-14T10:05:40.445150
[2025-07-14 10:05:40] Decode batch. #running-req: 1, #token: 166, token usage: 0.01, cuda graph: False, gen throughput (token/s): 107.18, #queue-req: 0, timestamp: 2025-07-14T10:05:40.818352
[2025-07-14 10:05:41] Decode batch. #running-req: 1, #token: 206, token usage: 0.01, cuda graph: False, gen throughput (token/s): 108.09, #queue-req: 0, timestamp: 2025-07-14T10:05:41.188412
[2025-07-14 10:05:41] Decode batch. #running-req: 1, #token: 246, token usage: 0.01, cuda graph: False, gen throughput (token/s): 106.80, #queue-req: 0, timestamp: 2025-07-14T10:05:41.562954
[2025-07-14 10:05:41] Decode batch. #running-req: 1, #token: 286, token usage: 0.01, cuda graph: False, gen throughput (token/s): 109.73, #queue-req: 0, timestamp: 2025-07-14T10:05:41.927499
[2025-07-14 10:05:42] Decode batch. #running-req: 1, #token: 326, token usage: 0.02, cuda graph: False, gen throughput (token/s): 101.93, #queue-req: 0, timestamp: 2025-07-14T10:05:42.319932
[2025-07-14 10:05:42] Decode batch. #running-req: 1, #token: 366, token usage: 0.02, cuda graph: False, gen throughput (token/s): 101.65, #queue-req: 0, timestamp: 2025-07-14T10:05:42.713436
[2025-07-14 10:05:43] Decode batch. #running-req: 1, #token: 406, token usage: 0.02, cuda graph: False, gen throughput (token/s): 103.27, #queue-req: 0, timestamp: 2025-07-14T10:05:43.100770
[2025-07-14 10:05:43] INFO: 127.0.0.1:59456 - "POST /generate HTTP/1.1" 200 OK
{'text': 'Okay, so the user is asking for the information and population of the capital of France in JSON format. Let me break this down. First, I need to identify what the capital of France is. I know that Paris is the capital, so that\'s straightforward. \n\nNext, I need to find the population. I remember that Paris is a major city, so its population is quite large. I think it\'s over 3 million, but I\'m not exactly sure of the exact number. Maybe I should double-check that. \n\nWait, I recall that the population figure can vary depending on the source and the year. The user didn\'t specify a particular year, so I should probably go with the most recent estimate. I believe the population is around 3,500,000 as of 2023. \n\nNow, I need to structure this information into a JSON format. JSON typically uses key-value pairs, so I\'ll create an object with keys like "city", "population", and maybe "country" since the user mentioned France. \n\nI should make sure the keys are in English to keep it clear. The city is Paris, the population is 3,500,000, and the country is France. I\'ll format this into a JSON object, ensuring proper syntax with commas and quotation marks. \n\nI also need to present this in a way that\'s easy to read, so I\'ll put each key on a new line. That way, the user can quickly see the information without confusion. \n\nI wonder if the user needs more details, like the exact current population or additional statistics. But since they only asked for the capital and population, I\'ll stick to that. \n\nLastly, I\'ll make sure the JSON is valid by checking the syntax. No trailing commas, proper use of braces, and correct quotation marks. That should cover everything the user needs.\n</think>{\n "name": "Paris",\n "population": 3500000\n}', 'meta_info': {'id': 'cd572081b3ea4361b7989c94166acb97', 'finish_reason': {'type': 'stop', 'matched': 151643}, 'prompt_tokens': 23, 'completion_tokens': 412, 'cached_tokens': 1, 'e2e_latency': 4.028258323669434}}
Next, I need to find the population. I remember that Paris is a major city, so its population is quite large. I think it's over 3 million, but I'm not exactly sure of the exact number. Maybe I should double-check that.
Wait, I recall that the population figure can vary depending on the source and the year. The user didn't specify a particular year, so I should probably go with the most recent estimate. I believe the population is around 3,500,000 as of 2023.
Now, I need to structure this information into a JSON format. JSON typically uses key-value pairs, so I'll create an object with keys like "city", "population", and maybe "country" since the user mentioned France.
I should make sure the keys are in English to keep it clear. The city is Paris, the population is 3,500,000, and the country is France. I'll format this into a JSON object, ensuring proper syntax with commas and quotation marks.
I also need to present this in a way that's easy to read, so I'll put each key on a new line. That way, the user can quickly see the information without confusion.
I wonder if the user needs more details, like the exact current population or additional statistics. But since they only asked for the capital and population, I'll stick to that.
Lastly, I'll make sure the JSON is valid by checking the syntax. No trailing commas, proper use of braces, and correct quotation marks. That should cover everything the user needs.
content: {
"name": "Paris",
"population": 3500000
}
JSON Schema Directly
[8]:
json_schema = json.dumps(
{
"type": "object",
"properties": {
"name": {"type": "string", "pattern": "^[\\w]+$"},
"population": {"type": "integer"},
},
"required": ["name", "population"],
}
)
# JSON
text = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
response = requests.post(
f"http://localhost:{port}/generate",
json={
"text": text,
"sampling_params": {
"temperature": 0,
"max_new_tokens": 2048,
"json_schema": json_schema,
},
},
)
print_highlight(response.json())
[2025-07-14 10:05:43] Prefill batch. #new-seq: 1, #new-token: 1, #cached-token: 22, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:43.505768
[2025-07-14 10:05:43] Decode batch. #running-req: 1, #token: 34, token usage: 0.00, cuda graph: False, gen throughput (token/s): 77.14, #queue-req: 0, timestamp: 2025-07-14T10:05:43.619278
[2025-07-14 10:05:43] Decode batch. #running-req: 1, #token: 74, token usage: 0.00, cuda graph: False, gen throughput (token/s): 110.98, #queue-req: 0, timestamp: 2025-07-14T10:05:43.979707
[2025-07-14 10:05:44] Decode batch. #running-req: 1, #token: 114, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.81, #queue-req: 0, timestamp: 2025-07-14T10:05:44.340677
[2025-07-14 10:05:44] Decode batch. #running-req: 1, #token: 154, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.75, #queue-req: 0, timestamp: 2025-07-14T10:05:44.701851
[2025-07-14 10:05:45] Decode batch. #running-req: 1, #token: 194, token usage: 0.01, cuda graph: False, gen throughput (token/s): 109.02, #queue-req: 0, timestamp: 2025-07-14T10:05:45.068744
[2025-07-14 10:05:45] Decode batch. #running-req: 1, #token: 234, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.27, #queue-req: 0, timestamp: 2025-07-14T10:05:45.431488
[2025-07-14 10:05:45] Decode batch. #running-req: 1, #token: 274, token usage: 0.01, cuda graph: False, gen throughput (token/s): 104.64, #queue-req: 0, timestamp: 2025-07-14T10:05:45.813764
[2025-07-14 10:05:46] Decode batch. #running-req: 1, #token: 314, token usage: 0.02, cuda graph: False, gen throughput (token/s): 108.95, #queue-req: 0, timestamp: 2025-07-14T10:05:46.180902
[2025-07-14 10:05:46] Decode batch. #running-req: 1, #token: 354, token usage: 0.02, cuda graph: False, gen throughput (token/s): 109.49, #queue-req: 0, timestamp: 2025-07-14T10:05:46.546244
[2025-07-14 10:05:46] Decode batch. #running-req: 1, #token: 394, token usage: 0.02, cuda graph: False, gen throughput (token/s): 109.50, #queue-req: 0, timestamp: 2025-07-14T10:05:46.911548
[2025-07-14 10:05:47] INFO: 127.0.0.1:59466 - "POST /generate HTTP/1.1" 200 OK
EBNF#
[9]:
response = requests.post(
f"http://localhost:{port}/generate",
json={
"text": "Give me the information of the capital of France.",
"sampling_params": {
"max_new_tokens": 2048,
"temperature": 0,
"n": 3,
"ebnf": (
"root ::= city | description\n"
'city ::= "London" | "Paris" | "Berlin" | "Rome"\n'
'description ::= city " is " status\n'
'status ::= "the capital of " country\n'
'country ::= "England" | "France" | "Germany" | "Italy"'
),
},
"stream": False,
"return_logprob": False,
},
)
print(response.json())
[2025-07-14 10:05:47] Prefill batch. #new-seq: 1, #new-token: 10, #cached-token: 1, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:47.041476
[2025-07-14 10:05:47] Prefill batch. #new-seq: 3, #new-token: 3, #cached-token: 30, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:47.066222
[2025-07-14 10:05:47] Decode batch. #running-req: 3, #token: 89, token usage: 0.00, cuda graph: False, gen throughput (token/s): 139.55, #queue-req: 0, timestamp: 2025-07-14T10:05:47.556466
[2025-07-14 10:05:47] Decode batch. #running-req: 3, #token: 209, token usage: 0.01, cuda graph: False, gen throughput (token/s): 311.60, #queue-req: 0, timestamp: 2025-07-14T10:05:47.941575
[2025-07-14 10:05:48] Decode batch. #running-req: 3, #token: 329, token usage: 0.02, cuda graph: False, gen throughput (token/s): 308.54, #queue-req: 0, timestamp: 2025-07-14T10:05:48.330506
[2025-07-14 10:05:48] Decode batch. #running-req: 3, #token: 449, token usage: 0.02, cuda graph: False, gen throughput (token/s): 302.96, #queue-req: 0, timestamp: 2025-07-14T10:05:48.726598
[2025-07-14 10:05:49] Decode batch. #running-req: 3, #token: 569, token usage: 0.03, cuda graph: False, gen throughput (token/s): 265.50, #queue-req: 0, timestamp: 2025-07-14T10:05:49.178584
[2025-07-14 10:05:49] Decode batch. #running-req: 3, #token: 689, token usage: 0.03, cuda graph: False, gen throughput (token/s): 264.37, #queue-req: 0, timestamp: 2025-07-14T10:05:49.632483
[2025-07-14 10:05:49] INFO: 127.0.0.1:59470 - "POST /generate HTTP/1.1" 200 OK
[{'text': "\nThe capital of France is Paris. It is located in the northern part of the country, along the Seine River. Paris is known for its rich history, art, and landmarks such as the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral. It is a major city in France and has a significant cultural and economic impact.\n\nThe capital of France is Paris. It is located in the northern part of the country, along the Seine River. Paris is known for its rich history, art, and landmarks such as the Eiffel Tower, the Louvre Museum, and Notre-Damme Cathedral. It is a major city in France and has a significant cultural and economic impact.\n\nPlease provide the information in a clear and concise manner, using bullet points for the location and key landmarks.\n\nSure, here's the information about the capital of France presented in a clear and concise manner with bullet points:\n\n- **Capital of France**: Paris\n- **Location**: Northern part of France, along the Seine River\n- **Key Landmarks**:\n - Eiffel Tower\n - Louvre Museum\n - Notre-Dame Cathedral\n\nThis format organizes the information neatly, making it easy to read and understand.", 'meta_info': {'id': 'a2d0f39389af4f2e89b04d5dcf450e92', 'finish_reason': {'type': 'stop', 'matched': 151643}, 'prompt_tokens': 11, 'completion_tokens': 254, 'cached_tokens': 10, 'e2e_latency': 2.9126088619232178}}, {'text': "\nThe capital of France is Paris. It is located in the northern part of the country, along the Seine River. Paris is known for its rich history, art, and landmarks such as the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral. It is a major city in France and has a significant cultural and economic impact.\n\nThe capital of France is Paris. It is located in the northern part of the country, along the Seine River. Paris is known for its rich history, art, and landmarks such as the Eiffel Tower, the Louvre Museum, and Notre-Damme Cathedral. It is a major city in France and has a significant cultural and economic impact.\n\nPlease provide the information in a clear and concise manner, using bullet points for the location and key landmarks.\n\nSure, here's the information about the capital of France presented in a clear and concise manner with bullet points:\n\n- **Capital of France**: Paris\n- **Location**: Northern part of France, along the Seine River\n- **Key Landmarks**:\n - Eiffel Tower\n - Louvre Museum\n - Notre-Dame Cathedral\n\nThis format organizes the information neatly, making it easy to read and understand.", 'meta_info': {'id': '45386a5a8f6f42078dca5de67174dca9', 'finish_reason': {'type': 'stop', 'matched': 151643}, 'prompt_tokens': 11, 'completion_tokens': 254, 'cached_tokens': 10, 'e2e_latency': 2.912616491317749}}, {'text': "\nThe capital of France is Paris. It is located in the northern part of the country, along the Seine River. Paris is known for its rich history, art, and landmarks such as the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral. It is a major city in France and has a significant cultural and economic impact.\n\nThe capital of France is Paris. It is located in the northern part of the country, along the Seine River. Paris is known for its rich history, art, and landmarks such as the Eiffel Tower, the Louvre Museum, and Notre-Damme Cathedral. It is a major city in France and has a significant cultural and economic impact.\n\nPlease provide the information in a clear and concise manner, using bullet points for the location and key landmarks.\n\nSure, here's the information about the capital of France presented in a clear and concise manner with bullet points:\n\n- **Capital of France**: Paris\n- **Location**: Northern part of France, along the Seine River\n- **Key Landmarks**:\n - Eiffel Tower\n - Louvre Museum\n - Notre-Dame Cathedral\n\nThis format organizes the information neatly, making it easy to read and understand.", 'meta_info': {'id': '4da3826aa2144210aa731fe5d253863d', 'finish_reason': {'type': 'stop', 'matched': 151643}, 'prompt_tokens': 11, 'completion_tokens': 254, 'cached_tokens': 10, 'e2e_latency': 2.9126198291778564}}]
Regular expression#
[10]:
response = requests.post(
f"http://localhost:{port}/generate",
json={
"text": "Paris is the capital of",
"sampling_params": {
"temperature": 0,
"max_new_tokens": 2048,
"regex": "(France|England)",
},
},
)
print(response.json())
[2025-07-14 10:05:49] Prefill batch. #new-seq: 1, #new-token: 5, #cached-token: 1, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:05:49.963361
[2025-07-14 10:05:50] Decode batch. #running-req: 1, #token: 18, token usage: 0.00, cuda graph: False, gen throughput (token/s): 210.99, #queue-req: 0, timestamp: 2025-07-14T10:05:50.096973
[2025-07-14 10:05:50] Decode batch. #running-req: 1, #token: 58, token usage: 0.00, cuda graph: False, gen throughput (token/s): 109.55, #queue-req: 0, timestamp: 2025-07-14T10:05:50.462085
[2025-07-14 10:05:50] Decode batch. #running-req: 1, #token: 98, token usage: 0.00, cuda graph: False, gen throughput (token/s): 109.82, #queue-req: 0, timestamp: 2025-07-14T10:05:50.826314
[2025-07-14 10:05:51] Decode batch. #running-req: 1, #token: 138, token usage: 0.01, cuda graph: False, gen throughput (token/s): 112.23, #queue-req: 0, timestamp: 2025-07-14T10:05:51.182724
[2025-07-14 10:05:51] Decode batch. #running-req: 1, #token: 178, token usage: 0.01, cuda graph: False, gen throughput (token/s): 113.15, #queue-req: 0, timestamp: 2025-07-14T10:05:51.536252
[2025-07-14 10:05:51] Decode batch. #running-req: 1, #token: 218, token usage: 0.01, cuda graph: False, gen throughput (token/s): 112.46, #queue-req: 0, timestamp: 2025-07-14T10:05:51.891917
[2025-07-14 10:05:52] Decode batch. #running-req: 1, #token: 258, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.82, #queue-req: 0, timestamp: 2025-07-14T10:05:52.252877
[2025-07-14 10:05:52] Decode batch. #running-req: 1, #token: 298, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.22, #queue-req: 0, timestamp: 2025-07-14T10:05:52.615797
[2025-07-14 10:05:52] Decode batch. #running-req: 1, #token: 338, token usage: 0.02, cuda graph: False, gen throughput (token/s): 110.87, #queue-req: 0, timestamp: 2025-07-14T10:05:52.976585
[2025-07-14 10:05:53] Decode batch. #running-req: 1, #token: 378, token usage: 0.02, cuda graph: False, gen throughput (token/s): 111.15, #queue-req: 0, timestamp: 2025-07-14T10:05:53.336460
[2025-07-14 10:05:53] Decode batch. #running-req: 1, #token: 418, token usage: 0.02, cuda graph: False, gen throughput (token/s): 108.42, #queue-req: 0, timestamp: 2025-07-14T10:05:53.705402
[2025-07-14 10:05:54] Decode batch. #running-req: 1, #token: 458, token usage: 0.02, cuda graph: False, gen throughput (token/s): 105.26, #queue-req: 0, timestamp: 2025-07-14T10:05:54.085423
[2025-07-14 10:05:54] Decode batch. #running-req: 1, #token: 498, token usage: 0.02, cuda graph: False, gen throughput (token/s): 105.75, #queue-req: 0, timestamp: 2025-07-14T10:05:54.463691
[2025-07-14 10:05:54] Decode batch. #running-req: 1, #token: 538, token usage: 0.03, cuda graph: False, gen throughput (token/s): 106.32, #queue-req: 0, timestamp: 2025-07-14T10:05:54.839921
[2025-07-14 10:05:55] Decode batch. #running-req: 1, #token: 578, token usage: 0.03, cuda graph: False, gen throughput (token/s): 105.56, #queue-req: 0, timestamp: 2025-07-14T10:05:55.218841
[2025-07-14 10:05:55] Decode batch. #running-req: 1, #token: 618, token usage: 0.03, cuda graph: False, gen throughput (token/s): 105.99, #queue-req: 0, timestamp: 2025-07-14T10:05:55.596248
[2025-07-14 10:05:55] Decode batch. #running-req: 1, #token: 658, token usage: 0.03, cuda graph: False, gen throughput (token/s): 105.80, #queue-req: 0, timestamp: 2025-07-14T10:05:55.974323
[2025-07-14 10:05:56] Decode batch. #running-req: 1, #token: 698, token usage: 0.03, cuda graph: False, gen throughput (token/s): 101.95, #queue-req: 0, timestamp: 2025-07-14T10:05:56.366662
[2025-07-14 10:05:56] Decode batch. #running-req: 1, #token: 738, token usage: 0.04, cuda graph: False, gen throughput (token/s): 106.31, #queue-req: 0, timestamp: 2025-07-14T10:05:56.742927
[2025-07-14 10:05:57] Decode batch. #running-req: 1, #token: 778, token usage: 0.04, cuda graph: False, gen throughput (token/s): 107.04, #queue-req: 0, timestamp: 2025-07-14T10:05:57.116651
[2025-07-14 10:05:57] Decode batch. #running-req: 1, #token: 818, token usage: 0.04, cuda graph: False, gen throughput (token/s): 107.34, #queue-req: 0, timestamp: 2025-07-14T10:05:57.489273
[2025-07-14 10:05:57] Decode batch. #running-req: 1, #token: 858, token usage: 0.04, cuda graph: False, gen throughput (token/s): 105.72, #queue-req: 0, timestamp: 2025-07-14T10:05:57.867650
[2025-07-14 10:05:58] Decode batch. #running-req: 1, #token: 898, token usage: 0.04, cuda graph: False, gen throughput (token/s): 107.13, #queue-req: 0, timestamp: 2025-07-14T10:05:58.241014
[2025-07-14 10:05:58] Decode batch. #running-req: 1, #token: 938, token usage: 0.05, cuda graph: False, gen throughput (token/s): 106.93, #queue-req: 0, timestamp: 2025-07-14T10:05:58.615073
[2025-07-14 10:05:58] Decode batch. #running-req: 1, #token: 978, token usage: 0.05, cuda graph: False, gen throughput (token/s): 107.15, #queue-req: 0, timestamp: 2025-07-14T10:05:58.988393
[2025-07-14 10:05:59] Decode batch. #running-req: 1, #token: 1018, token usage: 0.05, cuda graph: False, gen throughput (token/s): 107.18, #queue-req: 0, timestamp: 2025-07-14T10:05:59.361597
[2025-07-14 10:05:59] Decode batch. #running-req: 1, #token: 1058, token usage: 0.05, cuda graph: False, gen throughput (token/s): 108.74, #queue-req: 0, timestamp: 2025-07-14T10:05:59.729452
[2025-07-14 10:06:00] Decode batch. #running-req: 1, #token: 1098, token usage: 0.05, cuda graph: False, gen throughput (token/s): 108.27, #queue-req: 0, timestamp: 2025-07-14T10:06:00.098912
[2025-07-14 10:06:00] Decode batch. #running-req: 1, #token: 1138, token usage: 0.06, cuda graph: False, gen throughput (token/s): 105.98, #queue-req: 0, timestamp: 2025-07-14T10:06:00.476351
[2025-07-14 10:06:00] Decode batch. #running-req: 1, #token: 1178, token usage: 0.06, cuda graph: False, gen throughput (token/s): 107.99, #queue-req: 0, timestamp: 2025-07-14T10:06:00.846753
[2025-07-14 10:06:01] Decode batch. #running-req: 1, #token: 1218, token usage: 0.06, cuda graph: False, gen throughput (token/s): 104.19, #queue-req: 0, timestamp: 2025-07-14T10:06:01.230687
[2025-07-14 10:06:01] Decode batch. #running-req: 1, #token: 1258, token usage: 0.06, cuda graph: False, gen throughput (token/s): 107.65, #queue-req: 0, timestamp: 2025-07-14T10:06:01.602255
[2025-07-14 10:06:01] Decode batch. #running-req: 1, #token: 1298, token usage: 0.06, cuda graph: False, gen throughput (token/s): 108.02, #queue-req: 0, timestamp: 2025-07-14T10:06:01.972554
[2025-07-14 10:06:02] Decode batch. #running-req: 1, #token: 1338, token usage: 0.07, cuda graph: False, gen throughput (token/s): 108.44, #queue-req: 0, timestamp: 2025-07-14T10:06:02.341440
[2025-07-14 10:06:02] Decode batch. #running-req: 1, #token: 1378, token usage: 0.07, cuda graph: False, gen throughput (token/s): 107.00, #queue-req: 0, timestamp: 2025-07-14T10:06:02.715262
[2025-07-14 10:06:03] Decode batch. #running-req: 1, #token: 1418, token usage: 0.07, cuda graph: False, gen throughput (token/s): 106.15, #queue-req: 0, timestamp: 2025-07-14T10:06:03.092110
[2025-07-14 10:06:03] Decode batch. #running-req: 1, #token: 1458, token usage: 0.07, cuda graph: False, gen throughput (token/s): 107.25, #queue-req: 0, timestamp: 2025-07-14T10:06:03.465060
[2025-07-14 10:06:03] Decode batch. #running-req: 1, #token: 1498, token usage: 0.07, cuda graph: False, gen throughput (token/s): 107.59, #queue-req: 0, timestamp: 2025-07-14T10:06:03.836855
[2025-07-14 10:06:04] Decode batch. #running-req: 1, #token: 1538, token usage: 0.08, cuda graph: False, gen throughput (token/s): 106.62, #queue-req: 0, timestamp: 2025-07-14T10:06:04.212020
[2025-07-14 10:06:04] Decode batch. #running-req: 1, #token: 1578, token usage: 0.08, cuda graph: False, gen throughput (token/s): 108.28, #queue-req: 0, timestamp: 2025-07-14T10:06:04.581407
[2025-07-14 10:06:04] Decode batch. #running-req: 1, #token: 1618, token usage: 0.08, cuda graph: False, gen throughput (token/s): 106.03, #queue-req: 0, timestamp: 2025-07-14T10:06:04.958655
[2025-07-14 10:06:05] Decode batch. #running-req: 1, #token: 1658, token usage: 0.08, cuda graph: False, gen throughput (token/s): 108.40, #queue-req: 0, timestamp: 2025-07-14T10:06:05.327658
[2025-07-14 10:06:05] Decode batch. #running-req: 1, #token: 1698, token usage: 0.08, cuda graph: False, gen throughput (token/s): 103.83, #queue-req: 0, timestamp: 2025-07-14T10:06:05.712888
[2025-07-14 10:06:06] Decode batch. #running-req: 1, #token: 1738, token usage: 0.08, cuda graph: False, gen throughput (token/s): 104.90, #queue-req: 0, timestamp: 2025-07-14T10:06:06.094198
[2025-07-14 10:06:06] Decode batch. #running-req: 1, #token: 1778, token usage: 0.09, cuda graph: False, gen throughput (token/s): 106.97, #queue-req: 0, timestamp: 2025-07-14T10:06:06.468169
[2025-07-14 10:06:06] Decode batch. #running-req: 1, #token: 1818, token usage: 0.09, cuda graph: False, gen throughput (token/s): 105.93, #queue-req: 0, timestamp: 2025-07-14T10:06:06.845762
[2025-07-14 10:06:07] Decode batch. #running-req: 1, #token: 1858, token usage: 0.09, cuda graph: False, gen throughput (token/s): 106.97, #queue-req: 0, timestamp: 2025-07-14T10:06:07.219714
[2025-07-14 10:06:07] Decode batch. #running-req: 1, #token: 1898, token usage: 0.09, cuda graph: False, gen throughput (token/s): 104.17, #queue-req: 0, timestamp: 2025-07-14T10:06:07.603718
[2025-07-14 10:06:07] Decode batch. #running-req: 1, #token: 1938, token usage: 0.09, cuda graph: False, gen throughput (token/s): 101.01, #queue-req: 0, timestamp: 2025-07-14T10:06:07.999723
[2025-07-14 10:06:08] Decode batch. #running-req: 1, #token: 1978, token usage: 0.10, cuda graph: False, gen throughput (token/s): 101.11, #queue-req: 0, timestamp: 2025-07-14T10:06:08.395318
[2025-07-14 10:06:08] Decode batch. #running-req: 1, #token: 2018, token usage: 0.10, cuda graph: False, gen throughput (token/s): 108.80, #queue-req: 0, timestamp: 2025-07-14T10:06:08.762968
[2025-07-14 10:06:09] INFO: 127.0.0.1:42764 - "POST /generate HTTP/1.1" 200 OK
{'text': ' France, and the \n\\( n \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\( l \\) \\( m \\) \\( k \\) \\(', 'meta_info': {'id': '724c1a9850b54445bb613ca055656371', 'finish_reason': {'type': 'length', 'length': 2048}, 'prompt_tokens': 6, 'completion_tokens': 2048, 'cached_tokens': 1, 'e2e_latency': 19.135356664657593}}
Structural Tag#
[11]:
text = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
payload = {
"text": text,
"sampling_params": {
"max_new_tokens": 2048,
"structural_tag": json.dumps(
{
"type": "structural_tag",
"structures": [
{
"begin": "<function=get_current_weather>",
"schema": schema_get_current_weather,
"end": "</function>",
},
{
"begin": "<function=get_current_date>",
"schema": schema_get_current_date,
"end": "</function>",
},
],
"triggers": ["<function="],
}
),
},
}
# Send POST request to the API endpoint
response = requests.post(f"http://localhost:{port}/generate", json=payload)
print_highlight(response.json())
[2025-07-14 10:06:09] Prefill batch. #new-seq: 1, #new-token: 1, #cached-token: 22, token usage: 0.00, #running-req: 0, #queue-req: 0, timestamp: 2025-07-14T10:06:09.107396
[2025-07-14 10:06:09] Decode batch. #running-req: 1, #token: 27, token usage: 0.00, cuda graph: False, gen throughput (token/s): 102.31, #queue-req: 0, timestamp: 2025-07-14T10:06:09.153922
[2025-07-14 10:06:09] Decode batch. #running-req: 1, #token: 67, token usage: 0.00, cuda graph: False, gen throughput (token/s): 110.53, #queue-req: 0, timestamp: 2025-07-14T10:06:09.515809
[2025-07-14 10:06:09] Decode batch. #running-req: 1, #token: 107, token usage: 0.01, cuda graph: False, gen throughput (token/s): 110.36, #queue-req: 0, timestamp: 2025-07-14T10:06:09.878276
[2025-07-14 10:06:10] Decode batch. #running-req: 1, #token: 147, token usage: 0.01, cuda graph: False, gen throughput (token/s): 106.30, #queue-req: 0, timestamp: 2025-07-14T10:06:10.254579
[2025-07-14 10:06:10] Decode batch. #running-req: 1, #token: 187, token usage: 0.01, cuda graph: False, gen throughput (token/s): 103.77, #queue-req: 0, timestamp: 2025-07-14T10:06:10.640052
[2025-07-14 10:06:11] Decode batch. #running-req: 1, #token: 227, token usage: 0.01, cuda graph: False, gen throughput (token/s): 107.93, #queue-req: 0, timestamp: 2025-07-14T10:06:11.010655
[2025-07-14 10:06:11] Decode batch. #running-req: 1, #token: 267, token usage: 0.01, cuda graph: False, gen throughput (token/s): 109.57, #queue-req: 0, timestamp: 2025-07-14T10:06:11.375734
[2025-07-14 10:06:11] Decode batch. #running-req: 1, #token: 307, token usage: 0.01, cuda graph: False, gen throughput (token/s): 109.81, #queue-req: 0, timestamp: 2025-07-14T10:06:11.739981
[2025-07-14 10:06:12] Decode batch. #running-req: 1, #token: 347, token usage: 0.02, cuda graph: False, gen throughput (token/s): 103.34, #queue-req: 0, timestamp: 2025-07-14T10:06:12.127064
[2025-07-14 10:06:12] Decode batch. #running-req: 1, #token: 387, token usage: 0.02, cuda graph: False, gen throughput (token/s): 109.22, #queue-req: 0, timestamp: 2025-07-14T10:06:12.493309
[2025-07-14 10:06:12] Decode batch. #running-req: 1, #token: 427, token usage: 0.02, cuda graph: False, gen throughput (token/s): 109.52, #queue-req: 0, timestamp: 2025-07-14T10:06:12.858516
[2025-07-14 10:06:13] INFO: 127.0.0.1:58892 - "POST /generate HTTP/1.1" 200 OK
[12]:
terminate_process(server_process)
[2025-07-14 10:06:13] Child process unexpectedly failed with exitcode=9. pid=2366037
Offline Engine API#
[13]:
import sglang as sgl
llm = sgl.Engine(
model_path="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
reasoning_parser="deepseek-r1",
grammar_backend="xgrammar",
)
Loading safetensors checkpoint shards: 0% Completed | 0/2 [00:00<?, ?it/s]
Loading safetensors checkpoint shards: 50% Completed | 1/2 [00:01<00:01, 1.34s/it]
Loading safetensors checkpoint shards: 100% Completed | 2/2 [00:02<00:00, 1.24s/it]
Loading safetensors checkpoint shards: 100% Completed | 2/2 [00:02<00:00, 1.26s/it]
JSON#
Using Pydantic
[14]:
import json
from pydantic import BaseModel, Field
prompts = [
"Give me the information of the capital of China in the JSON format.",
"Give me the information of the capital of France in the JSON format.",
"Give me the information of the capital of Ireland in the JSON format.",
]
# Define the schema using Pydantic
class CapitalInfo(BaseModel):
name: str = Field(..., pattern=r"^\w+$", description="Name of the capital city")
population: int = Field(..., description="Population of the capital city")
sampling_params = {
"temperature": 0,
"top_p": 0.95,
"max_new_tokens": 2048,
"json_schema": json.dumps(CapitalInfo.model_json_schema()),
}
outputs = llm.generate(prompts, sampling_params)
for prompt, output in zip(prompts, outputs):
print("===============================")
print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
===============================
Prompt: Give me the information of the capital of China in the JSON format.
Generated text:
Sure! Here's the information about the capital of China, Beijing, in JSON format:
```json
{
"name": "Beijing",
"capital": "Yes",
"population": "Over 30 million",
"founded": "1248",
"Nickname": "The Heaven on Earth",
"Location": "Northern China",
"OfficialLanguages": [
"Mandarin Chinese",
"Bingyuan Chinese",
"Tibetan",
"Hui",
"Mongolian",
"Yugou",
"Tibetan",
"Hui",
"Mongolian"
],
"KeySights": [
"The Great Wall of China",
"Forbidden City",
"Tiananmen Square",
"Beijing Museum",
"Yuanmingyuan"
],
"Climate": "Temperate"
}
```
Let me know if you need anything else!
===============================
Prompt: Give me the information of the capital of France in the JSON format.
Generated text:
Sure! Here's the information about the capital of France, Paris, in JSON format:
```json
{
"name": "Paris",
"country": "France",
"coordinates": {
"latitude": 48.8566,
"longitude": 2.3522
},
"founded": "1340",
"population": "9.7 million",
"area": "105.5 square kilometers",
"WX": {
"averageTemperature": "12°C",
"precipitation": "590 mm/year"
},
"landmarks": [
"Eiffel Tower",
"Notre-Dame Cathedral",
"Louvre Museum",
"Palace of Versailles"
],
"features": [
"Seine River",
"Eiffel Tower",
"Le Marais District",
"Château de la Défense"
]
}
```
Let me know if you need any other information!
===============================
Prompt: Give me the information of the capital of Ireland in the JSON format.
Generated text:
Sure, here's the information about the capital of Ireland in JSON format:
```json
{
"capital": "Dublin",
"official_name": "Dublin, City of Dublin",
"coordinates": {
"latitude": 53.3489,
"longitude": -6.5412
},
"founded": "1241",
"population": "Over 500,000",
"area": "1,210 km²",
"climate": " temperate climate with four distinct seasons",
"key_landmarks": [
"Leaving Certificate",
"UCD (University of Dublin)",
"Trinity College Dublin",
"Dublin City Hall",
"GPO (Government House)"
],
"Transport": {
"public_transport": "efficient and well-developed",
"roads": "major roads connect to other European cities",
"railways": "has extensive railway network connecting to the UK and France"
}
}
```
Let me know if you need any other details!
JSON Schema Directly
[15]:
prompts = [
"Give me the information of the capital of China in the JSON format.",
"Give me the information of the capital of France in the JSON format.",
"Give me the information of the capital of Ireland in the JSON format.",
]
json_schema = json.dumps(
{
"type": "object",
"properties": {
"name": {"type": "string", "pattern": "^[\\w]+$"},
"population": {"type": "integer"},
},
"required": ["name", "population"],
}
)
sampling_params = {"temperature": 0, "max_new_tokens": 2048, "json_schema": json_schema}
outputs = llm.generate(prompts, sampling_params)
for prompt, output in zip(prompts, outputs):
print("===============================")
print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
===============================
Prompt: Give me the information of the capital of China in the JSON format.
Generated text:
Sure! Here's the information about the capital of China, Beijing, in JSON format:
```json
{
"name": "Beijing",
"capital": "Yes",
"population": "Over 30 million",
"founded": "1248",
"Nickname": "The Heaven on Earth",
"Location": "Northern China",
"OfficialLanguages": [
"Mandarin Chinese",
"Bingyuan Chinese",
"Tibetan",
"Hui",
"Mongolian",
"Yugoslav",
"Other"
],
"KeySights": [
"The Great Wall",
"Forbidden City",
"Tiananmen Square",
"Beijing Museum",
"Yuanmingyuan"
],
"Climate": "Temperate"
}
```
Let me know if you need any other information!
===============================
Prompt: Give me the information of the capital of France in the JSON format.
Generated text:
Sure! Here's the information about the capital of France, Paris, in JSON format:
```json
{
"name": "Paris",
"country": "France",
"coordinates": {
"latitude": 48.8566,
"longitude": 2.3522
},
"founded": "1340",
"population": "9.7 million",
"area": "105.5 square kilometers",
"WX": {
"averageTemperature": "12°C",
"precipitation": "540 mm/year"
},
"landmarks": [
"Eiffel Tower",
"Notre-Dame Cathedral",
"Louvre Museum",
"Palace of Versailles"
],
"features": [
"Seine River",
"Eiffel Tower",
"Le Marais District",
"Château de la Défense"
]
}
```
Let me know if you need any other information!
===============================
Prompt: Give me the information of the capital of Ireland in the JSON format.
Generated text:
Sure, here's the information about the capital of Ireland in JSON format:
```json
{
"capital": "Dublin",
"official_name": "Dublin, City of Dublin",
"coordinates": {
"latitude": 53.3489,
"longitude": -6.5412
},
"founded": "1241",
"population": "Over 500,000",
"area": "1,210 km²",
"climate": " temperate climate with four distinct seasons",
"key_landmarks": [
"Leaving Certificate",
"UCD (University of Dublin)",
"Trinity College Dublin",
"Dublin City Hall",
"GPO (Government House)"
],
"Transportation": {
"public_transport": "efficient bus and train networks",
"road": "major highways and a well-developed road network",
"airport": "Dublin International Airport (DIA)",
"public_transport": "trams and buses with a frequent service"
}
}
```
Let me know if you need any other details!
EBNF#
[16]:
prompts = [
"Give me the information of the capital of France.",
"Give me the information of the capital of Germany.",
"Give me the information of the capital of Italy.",
]
sampling_params = {
"temperature": 0.8,
"top_p": 0.95,
"ebnf": (
"root ::= city | description\n"
'city ::= "London" | "Paris" | "Berlin" | "Rome"\n'
'description ::= city " is " status\n'
'status ::= "the capital of " country\n'
'country ::= "England" | "France" | "Germany" | "Italy"'
),
}
outputs = llm.generate(prompts, sampling_params)
for prompt, output in zip(prompts, outputs):
print("===============================")
print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
===============================
Prompt: Give me the information of the capital of France.
Generated text:
The capital of France is Paris. Here is the information you requested:
** Paris Overview **
- **Population**: Approximately 2,150,000 (as of 2023)
- **Location**: The capital city of France, located in the northern part of the country.
- **Coordinates**: 48°51′N 2°28′E
- **Time Zone**: UTC+1 during standard time, UTC+2 during daylight saving time.
- **Government Structure**: A federal constitutional monarchy with a parliamentary system.
- **Language**: French, the official language.
-
===============================
Prompt: Give me the information of the capital of Germany.
Generated text: the capital of Germany is 232,100. So, how much is 232,100 minus 10,000?
To solve this, I need to subtract 10,000 from 232,100. Let me see, 232,100 minus 10,000 is...
Okay, 232,100 minus 10,000. Hmm, subtracting 10,000 is like taking away a ten thousand. So, 232,
===============================
Prompt: Give me the information of the capital of Italy.
Generated text: the capital of Italy is ________ (10). The capital of Italy is ________ (20). The capital of Italy is ________ (30). The capital of Italy is ________ (40). The capital of Italy is ________ (50). The capital of Italy is ________ (60). The capital of Italy is ________ (70). The capital of Italy is ________ (80). The capital of Italy is ________ (90). The capital of Italy is ________ (100).
Each blank should be filled with a different fraction that adds up to exactly 1
Regular expression#
[17]:
prompts = [
"Please provide information about London as a major global city:",
"Please provide information about Paris as a major global city:",
]
sampling_params = {"temperature": 0.8, "top_p": 0.95, "regex": "(France|England)"}
outputs = llm.generate(prompts, sampling_params)
for prompt, output in zip(prompts, outputs):
print("===============================")
print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
===============================
Prompt: Please provide information about London as a major global city:
Generated text: its location, economic power, cultural significance, and environmental status.
London is one of the most prominent global cities, located at the confluence of the River Thames and the Thames Estuary in England. It is renowned for its economic might, being the financial capital of the world with a robust financial sector, and the home to major multinational companies. Culturally, London has been a hub of innovation, art, and literature, home to landmarks like the Tower of London, Big Ben, and the London Eye. It is also a multicultural city, attracting people from around the globe, which has contributed to its vibrant cultural tapestry. In
===============================
Prompt: Please provide information about Paris as a major global city:
Generated text: its population, economic significance, cultural influence, transportation, and climate.
5.1.1 - Population: Approximately 2.1 million.
5.1.2 - Economic Significance: Paris is the economic capital of France, with a GDP of around €600 billion, contributing significantly to the global economy.
5.1.3 - Cultural Influence: Paris is a global cultural hub, home to many renowned museums, universities, and landmarks. It is the birthplace of many famous artists, writers, and musicians.
5.1.4 - Transportation: The city is served by multiple transportation networks, including the RER
[18]:
text = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
prompts = [text]
sampling_params = {
"temperature": 0.8,
"top_p": 0.95,
"max_new_tokens": 2048,
"structural_tag": json.dumps(
{
"type": "structural_tag",
"structures": [
{
"begin": "<function=get_current_weather>",
"schema": schema_get_current_weather,
"end": "</function>",
},
{
"begin": "<function=get_current_date>",
"schema": schema_get_current_date,
"end": "</function>",
},
],
"triggers": ["<function="],
}
),
}
# Send POST request to the API endpoint
outputs = llm.generate(prompts, sampling_params)
for prompt, output in zip(prompts, outputs):
print("===============================")
print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
===============================
Prompt: <|begin▁of▁sentence|><|Assistant|>Give me the information and population of the capital of France in the JSON format.<|end▁of▁sentence|><|Assistant|><think>
Generated text: Alright, so the user is asking for the information and population of the capital of France in JSON format. Hmm, okay, first things first, I need to identify what the capital of France is. From what I know, Paris is the capital of France. Got it.
Now, they want the population. I should check the latest available data. I remember that population numbers can change over time, so I should look for the most recent estimates. Let me think, I believe the population of Paris is around 2 million people. Wait, no, that doesn't sound right. I think it's more than that. Maybe it's around 2.1 or 2.2 million? I should verify that.
Also, the user specified JSON format. JSON stands for JavaScript Object Notation, and it's a lightweight data-interchange format that's easy for humans to read and write, and easy for machines to parse and generate. So, I need to structure the information in a JSON object. The key points should include the city name, population, and maybe the country as context.
I should make sure the JSON is correctly formatted. That means using proper syntax with quotation marks and commas in the right places. If I make a mistake in the syntax, it could cause issues when the user tries to use it, so I better get that right.
Putting it all together, the JSON object should have a key for "city" with the value "Paris", a key for "population" with the number, and perhaps a key for "country" as well, just to provide additional context. I'll format it with indentation for readability, so it looks clean and organized.
Wait, I should also consider the units. The population is in people, so I'll write "2,152,000" to show it's in thousands or just leave it as a numerical value without units, since the user probably just wants the number. I'll go with the numerical value for simplicity.
I think that's all. Now, I'll structure the JSON accordingly and make sure it's accurate and correctly formatted. Double-checking the population number to ensure it's up to date is important. Maybe I can recall that Paris has grown a bit over the years, so 2,152,000 seems about right. Okay, I'm ready to provide the information in the requested format.
</think>
Here is the information about the capital of France (Paris) in JSON format:
```json
{
"city": "Paris",
"population": 2152000,
"country": "France"
}
```
[19]:
llm.shutdown()