code: def generate_gbnf_grammar_from_pydantic_models(
      models: List[Type[BaseModel]],
      outer_object_name: str = None,
      outer_object_content: str = None,
      list_of_outputs: bool = False,
  ) -> str:
      """
      Generate GBNF Grammar from Pydantic Models.
  
      This method takes a list of Pydantic models and uses them to generate a GBNF grammar string. The generated grammar string can be used for parsing and validating data using the generated
      * grammar.
  
      Args:
          models (List[Type[BaseModel]]): A list of Pydantic models to generate the grammar from.
          outer_object_name (str): Outer object name for the GBNF grammar. If None, no outer object will be generated. Eg. "function" for function calling.
          outer_object_content (str): Content for the outer rule in the GBNF grammar. Eg. "function_parameters" or "params" for function calling.
          list_of_outputs (str, optional): Allows a list of output objects
      Returns:
          str: The generated GBNF grammar string.
  
      Examples:
          models = [UserModel, PostModel]
          grammar = generate_gbnf_grammar_from_pydantic(models)
          print(grammar)
          # Output:
          # root ::= UserModel | PostModel
          # ...
      """
      processed_models = set()
      all_rules = []
      created_rules = {}
      if outer_object_name is None:
          for model in models:
              model_rules, _ = generate_gbnf_grammar(
                  model, processed_models, created_rules
              )
              all_rules.extend(model_rules)
  
          if list_of_outputs:
              root_rule = (
                  r'root ::= (" "| "\n") "[" ws grammar-models ("," ws grammar-models)* ws "]"'
                  + "\n"
              )
          else:
              root_rule = r'root ::= (" "| "\n") grammar-models' + "\n"
          root_rule += "grammar-models ::= " + " | ".join(
              [
                  format_model_and_field_name(model.__name__)
                  for model in models
              ]
          )
          all_rules.insert(0, root_rule)
          return "\n".join(all_rules)
      elif outer_object_name is not None:
          if list_of_outputs:
              root_rule = (
                  rf'root ::= (" "| "\n") "[" ws {format_model_and_field_name(outer_object_name)} ("," ws {format_model_and_field_name(outer_object_name)})* ws "]"'
                  + "\n"
              )
          else:
              root_rule = f"root ::= {format_model_and_field_name(outer_object_name)}\n"
  
          model_rule = rf'{format_model_and_field_name(outer_object_name)} ::= (" "| "\n") "{{" ws "\"{outer_object_name}\""  ":" ws grammar-models'
  
          fields_joined = " | ".join(
              [
                  rf"{format_model_and_field_name(model.__name__)}-grammar-model"
                  for model in models
              ]
          )
  
          grammar_model_rules = f"\ngrammar-models ::= {fields_joined}"
          mod_rules = []
          for model in models:
              mod_rule = rf"{format_model_and_field_name(model.__name__)}-grammar-model ::= "
              mod_rule += (
                  rf'"\"{model.__name__}\"" "," ws "\"{outer_object_content}\"" ":" ws {format_model_and_field_name(model.__name__)}'
                  + "\n"
              )
              mod_rules.append(mod_rule)
          grammar_model_rules += "\n" + "\n".join(mod_rules)
  
          for model in models:
              model_rules, has_special_string = generate_gbnf_grammar(
                  model, processed_models, created_rules
              )
  
              if not has_special_string:
                  model_rules[0] += r'"\n" ws "}"'
  
              all_rules.extend(model_rules)
  
          all_rules.insert(
              0, root_rule + model_rule + grammar_model_rules
          )
          return "\n".join(all_rules)
  
imports: import inspect
  import json
  from copy import copy
  from inspect import isclass, getdoc
  from types import NoneType
  from docstring_parser import parse
  from pydantic import BaseModel, create_model, Field
  from typing import Any, Type, List, get_args, get_origin, Tuple, Union, Optional, _GenericAlias
  from enum import Enum
  from typing import get_type_hints, Callable
  import re
  from enum import Enum
label_desc: Use image node_id 10 for calling a global function with example usage: generate_gbnf_grammar_from_pydantic_models(models, outer_object_name, outer_object_content, list_of_outputs) and returns: str, str