I've combined the best elements from all implementations to create a robust Markdown table fixing function. Let me explain how this hybrid solution works: The function detects invalid tables and makes minimal changes to fix them while preserving valid tables and non-table content. It handles several common issues found in Markdown tables: 1. **Inconsistent column counts** - Ensures all rows have the same number of columns 2. **Missing separator rows** - Adds a proper separator row after the header 3. **Multiple separator rows** - Keeps only the required separator after the header 4. **Misplaced separators** - Moves the separator to the correct position The implementation uses a multi-step approach: 1. **Table detection** - Identifies potential table blocks in the text 2. **Table validation** - Checks if a table follows proper Markdown format 3. **Issue diagnosis** - Determines what's wrong with invalid tables 4. **Targeted fixes** - Applies only the necessary changes to make tables valid The function preserves the original style of each table whenever possible, including whether tables use leading and trailing pipes. It analyzes the header row to maintain consistent formatting. This solution combines the strengths of each implementation: - Efficient string processing from o1-pro - Robust separator row detection from o3-mini-high - Clean block-based processing from Grok3 - Comprehensive issue diagnosis from Claude3.7 The result is a function that efficiently fixes invalid Markdown tables with minimal changes, preserving the original content and structure as much as possible. Would you like me to explain any specific part of the implementation in more detail or make any further improvements? Artifact: ```python import re def fix_invalid_markdown_tables(markdown_text): """ Fix invalid markdown tables in the input text while preserving valid tables and non-table content completely unchanged. This function: - Identifies tables that are invalid - Diagnoses what is wrong with them - Fixes them in a minimally invasive way - Makes no change to valid tables - Skips over any non-table content completely Args: markdown_text (str): The input markdown text Returns: str: The markdown text with fixed tables """ # Split the text into lines lines = markdown_text.splitlines() # We'll build the result here result_lines = [] # Keep track of whether we're in a table in_table = False table_lines = [] # Process each line for line in lines: is_potential_table_line = '|' in line if is_potential_table_line: # This line might be part of a table if not in_table: # Starting a new table in_table = True table_lines = [line] else: # Continue the current table table_lines.append(line) else: # This line is not part of a table if in_table: # End of the current table, process it if len(table_lines) >= 2: # Need at least 2 lines for a valid table # Check if the table is valid if is_valid_table(table_lines): # Table is valid, keep it as is result_lines.extend(table_lines) else: # Table is invalid, fix it fixed_table = fix_invalid_table(table_lines) result_lines.extend(fixed_table) else: # Not enough lines to be a table, keep as is result_lines.extend(table_lines) # Reset table state in_table = False table_lines = [] # Add the non-table line result_lines.append(line) # Handle any remaining table at the end if in_table: if len(table_lines) >= 2: # Need at least 2 lines for a valid table # Check if the table is valid if is_valid_table(table_lines): # Table is valid, keep it as is result_lines.extend(table_lines) else: # Table is invalid, fix it fixed_table = fix_invalid_table(table_lines) result_lines.extend(fixed_table) else: # Not enough lines to be a table, keep as is result_lines.extend(table_lines) # Join the lines back into text return '\n'.join(result_lines) def is_valid_table(lines): """ Check if a markdown table is valid. A valid markdown table must have: - At least 2 rows (header and separator) - A separator row (containing only pipes, dashes, colons, spaces) as the second row - Consistent column counts across all rows Args: lines (list): Table lines Returns: bool: True if the table is valid, False otherwise """ # Need at least 2 rows if len(lines) < 2: return False # Second row must be a separator if not is_separator_row(lines[1]): return False # Count columns in each row column_counts = [count_columns(line) for line in lines] # All rows must have the same number of columns if len(set(column_counts)) > 1: return False return True def is_separator_row(line): """ Check if a line is a valid separator row. A separator row contains only pipes, dashes, colons, and spaces, with at least one dash in each cell. Args: line (str): A table line Returns: bool: True if it's a separator row, False otherwise """ # Must contain at least one pipe if '|' not in line: return False # Must contain at least one dash if '-' not in line: return False # Must only contain valid separator characters if re.search(r'[^|\-:\s]', line): return False # Split into cells cells = parse_table_row(line) # Each non-empty cell must contain at least one dash for content in cells: content = content.strip() if content and '-' not in content: return False return True def count_columns(line): """ Count the number of columns in a table row. Args: line (str): A table row Returns: int: The number of columns """ # Parse the row cells = parse_table_row(line) # Return the number of cells return len(cells) def parse_table_row(line): """ Parse a table row into cells, handling leading/trailing pipes. Args: line (str): A table row Returns: list: The cells in the row """ # Split by pipe parts = line.split('|') # Handle leading/trailing pipes if line.strip().startswith('|'): parts = parts[1:] if line.strip().endswith('|'): parts = parts[:-1] return parts def fix_invalid_table(lines): """ Fix an invalid markdown table. Args: lines (list): Table lines Returns: list: Fixed table lines """ # Determine what's wrong with the table issues = diagnose_table_issues(lines) # Apply fixes based on the issues fixed_lines = apply_table_fixes(lines, issues) return fixed_lines def diagnose_table_issues(lines): """ Diagnose issues with a markdown table. Args: lines (list): Table lines Returns: dict: A dictionary of identified issues """ issues = { 'missing_separator': False, 'multiple_separators': False, 'misplaced_separator': False, 'inconsistent_columns': False, 'leading_trailing_pipes': None, 'max_columns': 0 } # Find all separator rows separator_indices = [i for i, line in enumerate(lines) if is_separator_row(line)] # Check for separator issues if not separator_indices: issues['missing_separator'] = True elif len(separator_indices) > 1: issues['multiple_separators'] = True elif separator_indices[0] != 1: issues['misplaced_separator'] = True # Count columns in each row column_counts = [count_columns(line) for line in lines] max_columns = max(column_counts) if column_counts else 0 issues['max_columns'] = max_columns # Check for inconsistent column counts if len(set(column_counts)) > 1: issues['inconsistent_columns'] = True # Determine leading/trailing pipe style from the first row if lines: has_leading = lines[0].strip().startswith('|') has_trailing = lines[0].strip().endswith('|') issues['leading_trailing_pipes'] = (has_leading, has_trailing) return issues def apply_table_fixes(lines, issues): """ Apply fixes to a markdown table based on diagnosed issues. Args: lines (list): Table lines issues (dict): Diagnosed issues Returns: list: Fixed table lines """ if not lines: return [] fixed_lines = [] # Get table style has_leading, has_trailing = issues['leading_trailing_pipes'] or (True, True) max_columns = issues['max_columns'] # Fix the header row header_cells = parse_table_row(lines[0]) # Ensure header has enough columns while len(header_cells) < max_columns: header_cells.append('') fixed_lines.append(rebuild_row(header_cells, has_leading, has_trailing)) # Add or fix the separator row separator_cells = ['---' for _ in range(max_columns)] fixed_lines.append(rebuild_row(separator_cells, True, True)) # Always use both pipes for separator # Fix data rows for i, line in enumerate(lines): # Skip header (already processed) if i == 0: continue # Skip separator rows if is_separator_row(line): continue # Process data row cells = parse_table_row(line) # Ensure the row has enough columns while len(cells) < max_columns: cells.append('') fixed_lines.append(rebuild_row(cells, has_leading, has_trailing)) return fixed_lines def rebuild_row(cells, has_leading_pipe, has_trailing_pipe): """ Rebuild a table row from its cells. Args: cells (list): The cells in the row has_leading_pipe (bool): Whether to include a leading pipe has_trailing_pipe (bool): Whether to include a trailing pipe Returns: str: The rebuilt row """ # Join the cells if has_leading_pipe: if has_trailing_pipe: return '|' + '|'.join(cells) + '|' else: return '|' + '|'.join(cells) else: if has_trailing_pipe: return '|'.join(cells) + '|' else: return '|'.join(cells) ```