ALIA MrBERT Spanish Legal and Administrative Embeddings Model
This repository contains ALIA MrBERT Spanish Legal and Administrative Embeddings, a Spanish legal domain bi-encoder model for semantic similarity and information retrieval tasks. It is built upon MrBERT-es, a bilingual (Spanish–English) foundational language model based on the ModernBERT architecture, and fine-tuned on domain-specific legal and administrative data using a Curriculum Learning strategy.
[!WARNING]
DISCLAIMER: This model is a domain-specific proof-of-concept designed to demonstrate retrieval capabilities in the Spanish legal and administrative domain.
While optimized for this domain, results should be verified against official legal sources. The model may fail in out-of-domain or adversarial inputs.
📐 Architecture: ModernBERT with Mean Pooling (bi-encoder)
📏 Long context: up to 8,192 tokens
🎓 Training strategy: Curriculum Learning (easy → medium → hard)
⚙️ Negative mining: Positive-Aware Hard Negative Mining (NVIDIA approach)
Architecture
This model uses the same base architecture as MrBERT-es, extended with a Mean Pooling layer for sentence-level embeddings:
Base Architecture
ModernBERT
Total Parameters
~150M
Hidden size
768
Intermediate size
1,152
Attention heads
12
Hidden layers
22
Context length
8,192 tokens
Vocabulary size
51,200
Precision
bfloat16
Positional encoding
RoPE
Activation function
GeLU
Attention type
Mixed (global every 3 layers + sliding window)
Pooling strategy
Mean Pooling
Training
Training Strategy: Curriculum Learning
The model was fine-tuned using a two-phase Curriculum Learning strategy and progressively increasing the difficulty of training examples thanks to SINAI/ALIA-es-legal-administrative-triplets/train:
Phase
Epochs
Negative Type
Difficulty Progression
Phase 1
6
Random negatives
Easy → Medium → Hard
Phase 2
3
Hard negatives (mined)
Easy → Medium → Hard
Total
9
—
—
Phase 1 – Contrastive Learning with Random Negatives:
Training uses {query, relevant_doc, [irrelevant_docs]} with in-batch negatives. Examples are sorted by difficulty across 3 sub-phases (2 epochs each).
Phase 2 – Advanced Refinement with Hard Negatives:
Refinement using mined hard negatives with Positive-Aware Mining (NVIDIA approach) to avoid false negatives. A candidate is only considered a negative if:
score < score_positive - margin (margin = 0.05)
Hyperparameter Optimization
Before training, hyperparameter search was conducted using Optuna (20 trials, subsets of 5,000 examples):
CachedMultipleNegativesRankingLoss: Enables training with large batches (256) without VRAM overflow, by recalculating embeddings in smaller sub-batches (cache size: 4).
Training Framework
Component
Details
Library
sentence-transformers
Distributed
DDP (Distributed Data Parallel) via torchrun
Memory optimization
Gradient Checkpointing
Logging
WandB (offline mode)
Intended Use
Direct Use
This model is designed for semantic similarity and information retrieval tasks in the Spanish legal and administrative domain. Primary use cases include:
Semantic search: Finding relevant legal documents from a query
RAG pipelines: Generating context-enriched legal answers using retrieval-augmented generation
Legal document clustering: Grouping similar legal texts by semantic content
Duplicate detection: Identifying semantically similar legal clauses or articles
Out-of-Scope Use
General-domain retrieval (model is specialized for legal/administrative Spanish)
Cross-lingual retrieval beyond Spanish
Use as a generative model (this is an encoder-only model)
Legal advice or binding interpretations of legal texts
How to Use
With sentence-transformers
python
1from sentence_transformers import SentenceTransformer
23model = SentenceTransformer("SINAI/ALIA-MrBERT-es-legal-administrative-embeddings")45queries =["¿Cuáles son los requisitos para solicitar una prestación por desempleo?"]6documents =[7"El trabajador que cese en su actividad laboral tendrá derecho a la prestación por desempleo...",8"La prestación por desempleo contributiva se reconoce a quienes hayan cotizado al menos 360 días...",9]1011query_embeddings = model.encode(queries, prompt_name="query")12doc_embeddings = model.encode(documents)1314scores = model.similarity(query_embeddings, doc_embeddings)15print(scores)
The model was evaluated using the MTEB (Massive Text Embedding Benchmark) framework, adapted for the legal domain. The main reported metric is NDCG@10 (Normalized Discounted Cumulative Gain at k=10), which is the standard metric used in retrieval leaderboards and aligns with the metric reported in the MrBERT family.
An additional evaluation was made thanks to ragas evaluation framework and MiniMaxAI/MiniMax-M2.5 language model. These metrics are calculated by averaging on each pair puntuation from particular subsets of some of the following datasets.
Evaluation Datasets
Dataset
Category
Description
QA
Retrieval
Spanish subset of the MIRACL dataset in MTEB format (jinaai/miracl-es)
Note: Each evaluation subset is named following the pattern {dataset}_queries{N}_contexts{M}, where N is the number of queries evaluated against M contexts taken from the datasets.
Limitations and Biases
Known Limitations
Domain specificity: The model is optimized for Spanish legal and administrative texts. Performance may degrade significantly on general-domain or other specialized texts.
Language: Although MrBERT-es supports Spanish and English, this fine-tuned model focuses on Spanish legal content.
Legal accuracy: Semantic similarity does not guarantee legal correctness. Retrieved documents should always be verified by qualified professionals.
Context length: Despite supporting up to 8,192 tokens, very long documents may require chunking strategies for optimal retrieval performance.
Biases
The model may reflect biases present in the Spanish legal corpus used for training.
It may underperform on legal texts from Latin American jurisdictions, as training focused on Spanish national legislation and administration.
If you use this model in your research, please cite:
bibtex
1@misc{ALIA-MrBERT-es-legal-administrative-embeddings,
2 title = {ALIA MrBERT Spanish Legal and Administrative Embeddings Model},
3 author = {SINAI Research Group, Universidad de Jaén},
4 year = {2026},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/SINAI/ALIA-MrBERT-es-legal-administrative-embeddings}}
7}
Please also cite the base model:
bibtex
1@misc{tamayo2026mrbertmodernmultilingualencoders,
2 title={MrBERT: Modern Multilingual Encoders via Vocabulary, Domain, and Dimensional Adaptation},
3 author={Daniel Tamayo and Iñaki Lacunza and Paula Rivera-Hidalgo and Severino Da Dalt and Javier Aula-Blasco and Aitor Gonzalez-Agirre and Marta Villegas},
4 year={2026},
5 eprint={2602.21379},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL},
8 url={https://arxiv.org/abs/2602.21379},
9}
Funding
This work is funded by the Ministerio para la Transformación Digital y de la Función Pública - Funded by EU – NextGenerationEU within the framework of the project ALIA.
Acknowledgments
This dataset has been generated thanks to CEATIC (
Centro de Estudios Avanzados en Tecnologías de la Información y de la Comunicación) – UJA (Universidad de Jaén) which provided the needed computational resources on its clusters.